位置0错误没有行

时间:2013-10-23 00:32:14

标签: asp.net sql vb.net position row

我有以下代码:

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获取正确的行数。请帮忙:(

1 个答案:

答案 0 :(得分:4)

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

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