“索引0是负数或在行数以上”。错误C# [英] "Index 0 is either negative or above row count" error C#

查看:111
本文介绍了“索引0是负数或在行数以上”。错误C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要太多帮助。

我收到以下异常:


索引0为负或上面的行计数错误

Index 0 is either negative or above row count error

我在stackoverflow和google中进行了研究,但无法修复错误。

I researched in stackoverflow and google, but I couldn't fixed error.

我的代码:

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

但是此代码中没有出现错误:

But I am not getting an error in this code:

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

它们看起来一样。我很困惑。问题出在哪里?请告诉我。

They are looking same. I'm confused so much. Where is the problem? Tell me please.

推荐答案

这表示DataView, dataw 为空(查询不返回任何记录)。因此,您必须先检查是否为空,然后才能从中访问值。

Which means the DataView, dataw is Empty(the query returns no records) . So you have to Check for Empty before accessing the Value from it.

DataView dataw = new DataView(ReturnTable(gmyquery));
if (dataw!=null && dataw.Count > 0)
{
    // Your code here
}

这篇关于“索引0是负数或在行数以上”。错误C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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