Index was outside the bounds of the array ,why this error occur?
while i executing this i am facing that error
this is my coding
con.Open();
str = "select heading from newsmain12 where groupname='" + dr + "'";
cmd = new SqlCommand(str, con);
cmd.CommandType = CommandType.Text;
SqlDataReader odr = cmd.ExecuteReader();
while (odr.Read() == true)
{
idm += odr[0].ToString() + ",";
}
string[] ids1 = idm.Split(',');
if (ids1[0] != null)
{
Session["id1"] = ids1[0].ToString();
string m = Session["id1"].ToString();
}
if (ids1[1] != null)
{
Session["ids2"] = ids1[1].ToString();
string m1 = Session["ids2"].ToString();
}
else
{
}
Loading
Suthish NairPosted Mar 10, 2011, 1:19 PM
Suthish NairPosted Mar 9, 2011, 6:21 AM
Mamta MPosted Mar 9, 2011, 4:30 AM
MessageBox.Show(ids1.Length.ToString());
This will show you whether ids1 contains 2 rows as expected. Only then you can access the ids1[0] and ids1[1]. Otherwise, the array index will be outside bounds.
Amit ChoudharyPosted Mar 9, 2011, 4:09 AM
Change your condition if (ids1[0] != null) to if(String.IsNullOrEmpty(ids1[0]))
use it at both place for ids1[1] as well.
Hope this helps.
Thanks.