2013-11-25 22 views
1

這是我的代碼會話單值數據從數據庫和雙重價值的數據越來越不來

using (SqlCommand cmd = new SqlCommand("select * from Products where cast(ProductID as nvarchar(100)) in ('" + Session["Products"] + "')", con)) 
{ 
    con.Open(); 
    using (SqlDataReader sdr = cmd.ExecuteReader()) 
    { 
      while (sdr.Read()) 
      { 
       customers.Add(new ProductsData()); 
       customers[customers.Count - 1].ProductID = Convert.ToInt32(sdr["ProductID"].ToString()); 
       customers[customers.Count - 1].CategoryID = Convert.ToInt32(sdr["CategoryID"].ToString()); 
       customers[customers.Count - 1].Title = sdr["Title"].ToString(); 
       customers[customers.Count - 1].ThumbNail = sdr["ThumbNail"].ToString(); 
       customers[customers.Count - 1].IsActive = Convert.ToBoolean(sdr["IsActive"].ToString()); 
       customers[customers.Count - 1].FlashPath = sdr["FlashPath"].ToString(); 
       customers[customers.Count - 1].Price = Convert.ToDouble(sdr["Price"]); 
       customers[customers.Count - 1].Description = sdr["Description"].ToString(); 
       //customers[customers.Count - 1].CustomerID = Convert.ToInt32(sdr["customer_id"]); 
       //customers[customers.Count - 1].FirstName = sdr["firstname"].ToString(); 
       //customers[customers.Count - 1].LastName = sdr["lastname"].ToString(); 
      } 
     con.Close(); 
     //return customers; 
    } 
} 

這裏的時候Session["Products"]值,數據來了,就是while循環進入

Session["Products"]1,2,while循環沒有進入什麼是我的問題與此代碼PLZü可以建議我

小號分裂國家數據聲明更新

protected void chkFocusArea_CheckedChanged(object sender, EventArgs e) 
    { 
     CheckBox cb = (CheckBox)sender; 
     //update label count if checkbox is checked 
     ListViewItem item = (ListViewItem)cb.NamingContainer; 
     ListViewDataItem dataItem = (ListViewDataItem)item; 

     string code = productslist.DataKeys[dataItem.DisplayIndex].Value.ToString(); 
     //Header h1 = (Header)Page.LoadControl("~/UserControls/Header.ascx"); 

     //CitandPrototype.UserControls.Header h1 = new UserControls.Header(); 
     //Label mylabel = new Label(); 
     //mylabel=(Label)h1.FindControl("lblitems"); 


     string mylabel = ((Label)((this.Master).FindControl("Header")).FindControl("lblitems")).Text; 

     string mylabelitems = ((HiddenField)((this.Master).FindControl("Header")).FindControl("lblitemshidden")).Value; 

     if (!mylabelitems.Contains(code)) 

      if (mylabel.Length == 0) 
     { 
      // mylabelitems += "," + code; 
      mylabelitems = code; 
     } 
     else 
     { 
      // mylabelitems = code; 
      mylabelitems += ","+code; 
     } 

     int.TryParse(mylabel, out x); 

     if (cb.Checked) 
     { 
      x++; 
     } 
     else 
     { 
      if (x > 0) 
       x -= 1; 
      else 
       x = 0; 

      mylabelitems=mylabelitems.Replace(code, string.Empty); 

     } 
     ((Label)((this.Master).FindControl("Header")).FindControl("lblitems")).Text =x.ToString(); 
     ((HiddenField)((this.Master).FindControl("Header")).FindControl("lblitemshidden")).Value = mylabelitems.Trim(); 
     Session["ProductCount"] = x.ToString(); 
     Session["Products"] = mylabelitems.ToString(); 

    } 

回答

4

它,因爲這樣的:

using (SqlCommand cmd = new SqlCommand("select * from Products where cast(ProductID as nvarchar(100)) in ('" + Session["Products"] + "')", con)) 

您的最終查詢正在成爲:

select * from Products where cast(ProductID as nvarchar(100)) in ('1,2') 

,應該是:

select * from Products where cast(ProductID as nvarchar(100)) in ('1','2') 

select * from Products where ProductID in (1,2) 

嘗試爲寫:

using (SqlCommand cmd = new SqlCommand("select * from Products where ProductID in (" + Session['Products'] + ")", con)) 

編輯:

mylabelitems=mylabelitems.Replace(","+code, string.Empty); 
+0

如何獲得這種類型( '1', '2')在我的會議PLZ u能告訴我 – Karthik

+0

看到我終於提到了 –

+0

謝謝,我知道了,但是,首先我選擇了5個複選框,我得到了數據,當我取消選中3個複選框時出現錯誤「,」:錯誤的語法附近','。using(SqlDataReader sdr = cmd.ExecuteReader()) – Karthik