I’m trying to get to specific item in a ListBox (asp.net, C#)
and check if it’s null or not:
if (ListBox.Items[0] == null )
{
if (HowMany.Text == arrOfWords[0])
{
ListBox.Items.Add(arrOfWords[0]);
ErrorMessege.Text = "Good!";
}
}
it’s returning that : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Why is that?
Thanks!
Add a null check before accessing the array elements as well.
EDIT : From your comment “its ok that there are 0 items there for me, my intention is that if there is 0 items there, then it should add an Item from the arrOfWords“
So if you mean, even if there are zero items in the Listbox, you need to add an item from the array to listbox, then take out the First if condition