这是我的代码
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"]
值 1 时,数据即将来临并进入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();
}
答案 0 :(得分: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);