Вопрос

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
        }
    }
}
Это было полезно?

Решение

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
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top