Reputation: 73
I have values in listbox1, let say the values are hello, hammad, cricket. I want to compare this listbox with values in a table named oldTable. If I found the values in that table then //do something otherwise //do something else. I am using this code for comparing but this loop is not working correct.
while (sqlrdr.Read())
{
for (int i = 0; i <= listBox1.Items.Count - 1; i++)
{
if (listBox1.Items[i].ToString() == sqlrdr[0].ToString())
{
listBox1.SetSelected(i, true);
break; // TODO: might not be correct. Was : Exit For
}
}
}
Upvotes: 0
Views: 393
Reputation: 73472
It sounds like you need something like this. If this is not what you need then explain more clear what you're trying to acheive.
while (sqlrdr.Read())
{
string tableValue = sqlrdr[0].ToString();
bool found = listBox1.Items.Cast<object>().Any(x=>x.ToString() == tableValue);
if(found)
{
//Search found do whatever
}
else
{
//Search not found do whatever
}
}
Upvotes: 2