vb.net 位置 0 处没有行错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19530733/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:35:13  来源:igfitidea点击:

There is no row at position 0 error

asp.netsqlvb.netpositionrow

提问by Poch

I have the following code:

我有以下代码:

Do Until i = dsSalesRep.Tables(0).Rows.Count
                strRepID = dsSalesRep.Tables(0).Rows(i)("repID").ToString
                strLBP = dsSalesRep.Tables(0).Rows(i)("officename").ToString
                strLBPEmail = dsSalesRep.Tables(0).Rows(i)("cemail").ToString
                strCCRepEmail = dsSalesRep.Tables(0).Rows(i)("cc1").ToString
                strRepName = dsSalesRep.Tables(0).Rows(i)("firstname").ToString & " " & dsSalesRep.Tables(0).Rows(i)("lastname").ToString
                strRepEmail = dsSalesRep.Tables(0).Rows(i)("email").ToString
                strRepPhone = dsSalesRep.Tables(0).Rows(i)("phone").ToString

                searchStr3 = "SELECT * FROM LookupSalesRep WHERE repID='" & strRepID & "'"
                Dim SqlAdapter3 As New SqlDataAdapter(searchStr3, myConn2)
                Dim dsTerritories As New DataSet
                myConn2.Open()
                SqlAdapter3.Fill(dsTerritories)
                strCountry = dsTerritories.Tables(0).Rows(0)("country").ToString
                'strCountry = dsTerritories.Tables(0).Rows.Count.ToString

When I run the web page, though, it gives me the error: There is no row at position 0.

但是,当我运行网页时,它给了我错误:位置 0 处没有行。

But when I use the code

但是当我使用代码时

strCountry = dsTerritories.Tables(0).Rows.Count.ToString

strCountry gets the right number of rows. Please help :(

strCountry 获取正确的行数。请帮忙 :(

回答by afzalulh

You are in a loop and at some point the table (dsTerritories.Tables(0)) is null. Check first if the table is null:

您处于循环中,并且在某些时候表 (dsTerritories.Tables(0)) 为空。首先检查表是否为空:

If dsTerritories.Tables(0).Rows.Count > 0 Then
    strCountry = dsTerritories.Tables(0).Rows(0)("country").ToString
End If