我需要太多帮助。
我收到以下异常:
索引0为负数或高于行数错误
我在stackoverflow和google中进行了研究,但我无法修复错误。
我的代码:
if (lb_Gmy.SelectedItems.Count > 0)
{
string gmyquery = "Select * from tblA (nolock) where ParameterID = 2 and Status = 1";
DataView dataw = new DataView(ReturnTable(gmyquery));
foreach (string item in lb_Gmy.SelectedItems)
{
dataw.RowFilter = "Value = '" + item + "'";
gmy += dataw[0]["ParamValue"].ToString() + ",";
}
gmy = gmy.Substring(0, gmy.Length - 1);
}
但我在这段代码中没有收到错误:
if (lb_Fg.SelectedItems.Count > 0)
{
string gmyquery = "Select * from tblA (nolock) where ParameterID = 2 and Status = 1";
DataView dataw = new DataView(ReturnTable(gmyquery));
foreach (string item in lb_Fg.SelectedItems)
{
dataw.RowFilter = "Value = '" + item + "'";
findingGmy += dataw[0]["ParamValue"].ToString() + ",";
}
findingGmy = findingGmy.Substring(0, findingGmy.Length - 1);
}
他们看起来一样。我很困惑。问题出在哪儿?请告诉我。
答案 0 :(得分:2)
这意味着DataView,dataw
为空(查询不返回任何记录)。因此,在从中访问Value之前,您必须检查Empty。
DataView dataw = new DataView(ReturnTable(gmyquery));
if (dataw!=null && dataw.Count > 0)
{
// Your code here
}