System.IndexOutOfRangeException
问题描述:
我想显示我的复选框列表中的每个复选框的工具提示。我需要描述来自数据库。描述很长,但我得到一个System.IndexOutOfRangeException
。有谁知道如何解决这一问题?请帮忙!System.IndexOutOfRangeException
int i = 0;
foreach (ListItem l in this.agile_factors.Items) {
while (dr.Read())
{
string description = dr["Description"].ToString();
l.Attributes["title"] = description;
}
i++;
}
conn.Close();
答
这只是不起作用。
1)您的while
循环将在foreach
循环的第一次迭代中读取整个数据读取器。第一个listitem的l.Attributes["title"]
只会等于最后一个记录的值,那么您应该在下一个foreach
迭代中读取数据读取器之后读取数据读取器时发生错误。虽然我不认为这是“索引超出范围”。
2)索引超出范围可能是因为数据读取器没有包含您在其中使用的名称之一的列。
打开调试器应该很快就清除它。
答
它是否像试图使用它之前检查列的存在一样简单?
foreach (ListItem l in this.agile_factors.Items) {
while (dr.Read())
{
if (dr["Description"] != null)
{
string description = dr["Description"].ToString();
l.Attributes["title"] = description;
}
}
i++;
}
你会得到什么样的例外? – Jeff 2011-02-24 22:19:06
你在哪里得到例外。我有这种感觉,我们错过了一些重要的代码... – rene 2011-02-24 22:20:03
同上2条评论。你是否也知道我会永远等于agile_factors.Items.Size。 – 2011-02-24 22:21:56