Why my loop can not end? Why does it throw an exception instead?
int i=0;
ArrayList item = new ArrayList();
ArrayList list = new ArrayList();
while (reader.Read())
{
item.Add(reader["element"].ToString());//keep data from my SQL
}
string chk2 = textBox1.Text.ToString();
for ( i = 0; i <= item.Count;i++ )
{
if ((item[i].ToString()).Contains(chk2) )//this line got error.
{
list.Add(item[i]);
MessageBox.Show(item[i].ToString());
}
else
{
MessageBox.Show("Not Found");
}
}
Error note:An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
How i fix it please?