Pass and Retrieve the values of listbox in C#.net Windows Application
I want to insert multiple selected values into database in listbox.
I am using Windows application in C#.net.
I tried following code but did not work.
string ProductIDList;
SqlCommand com = new SqlCommand("Select ProductID from Product_Category where CategoryID=" + Catid +"" , con);
dr = com.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
if (ProductIDList == "")
{
ProductIDList = dr["ProductID"].ToString();
}
else
{
ProductIDList = ProductIDList + "," + dr["ProductID"].ToString();
}
ProductIDList = ProductIDList.Substring(1, ProductIDList.Length - 1开发者_StackOverflow社区);
}
dr.Close();
var temparry = ProductIDList.Split(new string[] { "," }, StringSplitOptions.None);
foreach (ListItem li in listBox1.Items)
{
foreach (string s in temparry)
{
if (li.Value == s)
li.Selected = true;
}
}
In this code Foreach section ListItem not working in Windows application. I've done this thing before in asp.net application , But how can I do in Desktop(Windows) application?
Please help.
for (int i = 0; i < listBox1.Items.Count ; i++)
{
if (listbox1.Items[i].ToString() == s)
btw, I would also look at some refactoring. Arent there too many loops going on there?
精彩评论