i have this code:
ArrayList list = new ArrayList();
foreach (DataRow dataR in prenume.Rows)
{
foreach (var item in dataR.ItemArray)
{
if (item.Equals(" ")) continue;
list.Add(item);
if (input_string.Equals(item.ToString()) && list.Count > 0 )
{
label_hello.Text = "Hello, " + list[2];
}
}
}
When i’m trying to clear the text showed , i get an error which says:
Index was out of range. Must be non-negative and less than the size
of the collection.
Later edit:
Solution found!I was too tired ... sorry for the question!
Well, you start off with an empty list, and then after adding a single item, you might execute (if
input_stringequals the first item in the first item array):That’s trying to access the third item in the list. It will fail when there’s only one item. Why did you pick
2here?(As an aside, why are you using
ArrayList? The genericList<T>type is preferred.)It’s not clear what you’re trying to achieve – if you can give us more context, we have a better chance of helping you.
EDIT: From the comments, it looks like this should be
However, I suspect the loops are still not right… why would you want to iterate over every value in the table, rather than (say) in just one column?