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

Amit ChoudharyRank #145 · 15.8K reputationPosted Mar 8, 2011, 11:09 PM EST
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.
Mamta MRank #268 · 9.4K reputationPosted Mar 8, 2011, 11:30 PM EST
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.
Suthish NairRank #92 · 22.8K reputationPosted Mar 9, 2011, 1:21 AM EST
Suthish NairRank #92 · 22.8K reputationPosted Mar 10, 2011, 8:19 AM EST