简体   繁体   English

出现错误“位置 0 处没有行”

[英]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"请帮我解决这个问题...我收到错误,因为“位置 0 没有行”、“索引超出范围异常未由用户代码处理”

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;
        }
    }
}

You're not checking if your tables have any content.您没有检查您的表格是否有任何内容。 The message is clear: There is no row at position 0.消息很明确:位置 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...
}

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.但是,在我看来很明显,位置 0 处没有行的原因是您正在查看错误的表。 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.我严重怀疑您在一个表中有 id,在另一个表中有 name,等等,但是您正在为每条数据索引到不同的表。

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!您肯定只有一张桌子,但正在查看表 0 到表 6!

Before accessing anything always check for items first.在访问任何东西之前,总是先检查项目。 After you do that and if this still occurs check the column names are they exact?这样做之后,如果仍然发生这种情况,请检查列名是否准确? For example is "TaskID" really "TaskId" and that indexing is failing?例如, "TaskID"真的是"TaskId"并且索引失败? Finally are these items nullable?最后这些项目可以为空吗? If so calling a ToString() on a null field is not what you want.如果是这样,在空字段上调用 ​​ToString() 不是您想要的。

检查您的数据表是否为空

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM