3

我有以下代码:

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

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

但是当我使用代码时

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

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

4

1 回答 1

4

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

If dsTerritories.Tables(0).Rows.Count > 0 Then
    strCountry = dsTerritories.Tables(0).Rows(0)("country").ToString
End If
于 2013-10-23T03:19:08.707 回答