Asp.net 从会话中检索多个值以填充列表框
我正在尝试从会话变量中检索多个值,我将这些值存储为列表。这是我应用的代码,但这只提供了输出列表中的lst值。我真的非常感谢你的帮助Asp.net 从会话中检索多个值以填充列表框,asp.net,Asp.net,我正在尝试从会话变量中检索多个值,我将这些值存储为列表。这是我应用的代码,但这只提供了输出列表中的lst值。我真的非常感谢你的帮助 Array k= yourlist.ToArray(); for (Int32 i = 0; i < k.Length; i++) { Int32 x = Convert.ToInt32(k.GetValue(i)); SqlCommand cmd2 = new SqlComman
Array k= yourlist.ToArray();
for (Int32 i = 0; i < k.Length; i++)
{
Int32 x = Convert.ToInt32(k.GetValue(i));
SqlCommand cmd2 = new SqlCommand("select id,name from plugins where id =" + x, con);
SqlDataReader dr2 = cmd2.ExecuteReader();
if (dr2.HasRows)
{
while (dr.Read())
{
ListBox2.DataSource = dr2;
ListBox2.DataBind();
}
}
dr2.Close();
cmd2.Dispose();
}
Array k=yourlist.ToArray();
对于(Int32 i=0;i
谢谢 列表框一次只能绑定到一个对象。声明主列表,扫描数组,在循环中运行sql,将结果添加到主列表,然后(在循环外部)将ListBox2绑定到主列表。首先执行多个查询,每个查询返回一行,然后在循环中重新绑定。您应该在一个查询中获取所有行,然后将结果绑定到控件 您可能会更幸运地使用以下内容:
List<string> ids = yourlist.Select(o => o.ToString()).ToList();
string idList = string.Join(",", ids);
SqlCommand cmd2 = new SqlCommand("select id,name from plugins where id in ("+idList+")", con);
using( SqlDataReader dr2 = cmd2.ExecuteReader() )
{
if( dr2.HasRows)
{
ListBox2.DataSource = dr2;
ListBox2.DataBind();
}
}
List id=yourlist.Select(o=>o.ToString()).ToList();
string idList=string.Join(“,”,id);
SqlCommand cmd2=新的SqlCommand(“从id位于(“+idList+”)中的插件中选择id、名称”,con);
使用(SqlDataReader dr2=cmd2.ExecuteReader())
{
if(dr2.HasRows)
{
ListBox2.DataSource=dr2;
ListBox2.DataBind();
}
}