"There is no row at position 0" error.

Posted by Krvish under ASP.NET on 2/5/2017 | Points: 10 | Views : 457 | Status : [Member] | Replies : 1
I was trying to fetch the "Age" from the table "tbl_Stud". The code is below. Earlier the line " lblAge.Text = dt.Rows[0]["Age"].ToString(); " was displaying a " There is no row at position 0. " error. Then I tried defending it like this,
   if(dt.Rows.Count > 0)
{
lblAge.Text = dt.Rows[0]["Age"].ToString();
}
else
{
lblAge.Text = "0";
}


and now, the code displays a 0 for every entry.

Please help me.




Responses

Posted by: A2H on: 2/5/2017 [Member] [MVP] Silver | Points: 25

Up
0
Down
If you have multiple rows in datatable. You might cosider looping thorugh the rows and assign values

for (int i = 0; i <= dt.Rows.Count; i++)
{
if (dt.Rows.Count > 0)
{
lblAge.Text = dt.Rows[i]["Age"].ToString();
}
else
{
lblAge.Text = "0";
}
}


Thanks,
A2H
My Blog

Krvish, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response