得到错误"没有排在位置0" [英] Getting error "there is no row at position 0"

查看:95
本文介绍了得到错误"没有排在位置0"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我弄清楚这个......我得到的错误是没有排在位置0,索引超出范围的异常是由用户代码unhanded



下面是我的代码

 保护无效的Page_Load(对象发件人,EventArgs五)
{
MTMSService的obj =新MTMSService();
DBACCESS DB =新DBACCESS();
{
MTMSDTO objc =新MTMSDTO();
{
objc.TaskID = Convert.ToInt32(会话[的TaskID]);
的DataSet代表= obj.GetReports(objc);
数据视图Rprts =新的数据视图();
Rprts.Table = rep.Tables [0];

LblTask​​ID.Text = rep.Tables [0] .Rows [0] [的TaskID]的ToString()。
LblTask​​Name.Text = rep.Tables [1] .Rows [0] [TASKNAME]的ToString();
LblDueDate.Text = rep.Tables [2] .Rows [0] [DUEDATE]的ToString();
LblDescription.Text = rep.Tables [3] .Rows [0] [描述]的ToString();
LblAssignBy.Text = rep.Tables [4] .Rows [0] [AssignBy]的ToString();
LblStatus.Text = rep.Tables [5] .Rows [0] [状态]的ToString();
LblPercentageComplete.Text =
rep.Tables [6] .Rows [0] [PercentageComplete]的ToString();

LblTask​​Name.Visible = TRUE;
LblAssignBy.Visible = TRUE;
LblDescription.Visible = TRUE;
LblDueDate.Visible = TRUE;
LblStatus.Visible = TRUE;
LblPercentageComplete.Visible = TRUE;
LblAssignTo.Visible = FALSE;
}
}
}


解决方案

你不检查,如果你的表已经得到的任何内容。信息是明确的 - 有从0位置没有排它可能是被扔在这条线上,还是其后之一:

  LblTask​​ID.Text = rep.Tables [0] .Rows [0] [的TaskID]的ToString(); 

您应该检查是否有试图从他们那里得到数据前行。喜欢的东西(验证语法使用此之前 - 这不是在所有测试):

  VAR表= rep.Tables [ 0]; 
如果(table.Rows.Count大于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;
        }
    }
}

解决方案

You're not checking if your tables have got any content. The message is clear - there is no row at position 0. It is probably being thrown on this line, or one following it:

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

You should check that there are rows before attempting to get data from them. Something like (verify the syntax before you use this - this is not tested at all):

var table = rep.Tables[0];
if (table.Rows.Count > 0){
    // Fetch the data... 
}
else
{
    // Handle missing data in an appropriate way...
}

这篇关于得到错误"没有排在位置0"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆