Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error "there is no row at position 0"

please help me out to sort out this... I'm getting error as "there is no row at position 0", "index out of range exception was unhanded by user code"

Below is my code

protected void Page_Load(object sender, EventArgs e)
{
    MTMSService obj = new MTMSService();
    DBAccess db = new DBAccess();
    {
        MTMSDTO objc = new MTMSDTO();
        {
            objc.TaskID = Convert.ToInt32(Session["TaskID"]);
            DataSet rep = obj.GetReports(objc);
            DataView Rprts = new DataView();
            Rprts.Table = rep.Tables[0];

            LblTaskID.Text = rep.Tables[0].Rows[0]["TaskID"].ToString();
            LblTaskName.Text = rep.Tables[1].Rows[0]["TaskName"].ToString();
            LblDueDate.Text = rep.Tables[2].Rows[0]["DueDate"].ToString();
            LblDescription.Text = rep.Tables[3].Rows[0]["Description"].ToString();
            LblAssignBy.Text = rep.Tables[4].Rows[0]["AssignBy"].ToString();
            LblStatus.Text = rep.Tables[5].Rows[0]["Status"].ToString();
            LblPercentageComplete.Text = 
                    rep.Tables[6].Rows[0]["PercentageComplete"].ToString();

            LblTaskName.Visible = true;
            LblAssignBy.Visible = true;
            LblDescription.Visible = true;
            LblDueDate.Visible = true;
            LblStatus.Visible = true;
            LblPercentageComplete.Visible = true;
            LblAssignTo.Visible = false;
        }
    }
}
like image 516
Suraj Avatar asked Jun 27 '13 12:06

Suraj


2 Answers

You're not checking if your tables have any content. The message is clear: There is no row at position 0.

The exception is probably being thrown on this line, or one following it:

LblTaskID.Text = rep.Tables[0].Rows[0]["TaskID"].ToString();

You should verify that rows exist before attempting to get data from them. Something like the following:

var table = rep.Tables[0];
if (table.Rows.Count > 0){
    // Fetch the data... 
}
else
{
    // Handle missing data in an appropriate way...
}
like image 50
Kjartan Avatar answered Nov 12 '22 02:11

Kjartan


The earlier advice is all good and you should follow it.

However it looks obvious to me that the reason there is no row at position 0 is that you are looking at the wrong table. I seriously doubt you have id in one table, name in another, etc., but you are indexing to a different table for each piece of data.

rep.Tables[1]
rep.Tables[2]
rep.Tables[3]
rep.Tables[4]
rep.Tables[5]
rep.Tables[6]

should all be

rep.Tables[0]

You surely only have one table, but are looking at table 0 through table 6!

like image 41
KennyZ Avatar answered Nov 12 '22 03:11

KennyZ