gpt4 book ai didi

c# - 单值数据的 session 正在从数据库获取,而双值数据的 session 未到来

转载 作者:搜寻专家 更新时间:2023-10-30 21:59:44 24 4
gpt4 key购买 nike

这是我的代码

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 时,循环没有进入我的代码问题是什么 Plz 你能给我建议吗

session 数据声明已更新

 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();

}

最佳答案

这是因为:

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);

关于c# - 单值数据的 session 正在从数据库获取,而双值数据的 session 未到来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20192623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com