C# 出现错误“位置 0 处没有行”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17343274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting error "there is no row at position 0"
提问by Suraj
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;
}
}
}
采纳答案by Kjartan
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...
}
回答by ΩmegaMan
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? Finally are these items nullable? If so calling a ToString() on a null field is not what you want.
在访问任何东西之前,总是先检查项目。执行此操作后,如果仍然发生这种情况,请检查列名称是否准确?例如是"TaskID"真的"TaskId",索引失败了?最后这些项目可以为空吗?如果是这样,在空字段上调用 ToString() 不是您想要的。
回答by KennyZ
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.
但是,在我看来很明显,位置 0 处没有行的原因是您正在查看错误的表。我严重怀疑您在一个表中有 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!
回答by rinku Choudhary
check that your datatable is empty
检查您的数据表是否为空

