我从下面的代码得到错误“位置0没有行”

时间:2013-08-23 11:44:43

标签: vb.net

Private Sub Button3_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    If txtID.Text = "" Then
        MsgBox("Please input a valid Employee code to load a corresponding record", MsgBoxStyle.Information)
    Else
        dbProvider = "Provider=Microsoft.Ace.OLEDB.12.0;"
        dbSource = "Data Source = C:\Users\Blessing\Documents\IBCARIP.accdb;Persist Security Info=False"
        con.ConnectionString = dbProvider & dbSource
        con.Open()
        sql = "select * from Calculator where " _
            & "EmpCode = " & " '" & txtID.Text & "'"

        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "IBCARIP")
        lblSAI.Text = ds.Tables("IBCARIP").Rows(inc).Item("SName") & ds.Tables("IBCARIP").Rows(inc).Item("FName")
        lblRate.Text = ds.Tables("IBCARIP").Rows(inc).Item("NRate")
        lblOT.Text = ds.Tables("IBCARIP").Rows(inc).Item("OTRate")
        lblBnk.Text = ds.Tables("IBCARIP").Rows(inc).Item("BName") & ".." & ds.Tables("IBCARIP").Rows(inc).Item("ANumber") & ".." & ds.Tables("IBCARIP").Rows(inc).Item("AType")
        con.Close()
        ds.Tables("IBCARIP").DataSet.Clear()
        MaxRows = ds.Tables("IBCARIP").Rows.Count
        'inc = 0
    End If
End Sub

当我在txtID.text

中输入错误或不存在的员工代码时,会出现此消息

我该如何解决问题

2 个答案:

答案 0 :(得分:1)

尝试如下

您应该始终检查数据集表和行数

我不太熟悉vb .net(我在C#中),但我认为以下是好的去

    If txtID.Text = "" Then
        MsgBox("Please input a valid Employee code to load a corresponding record", MsgBoxStyle.Information)
    Else
        dbProvider = "Provider=Microsoft.Ace.OLEDB.12.0;"
        dbSource = "Data Source = C:\Users\Blessing\Documents\IBCARIP.accdb;Persist Security Info=False"
        con.ConnectionString = dbProvider & dbSource
        con.Open()
        sql = "select * from Calculator where " _
            & "EmpCode = " & " '" & txtID.Text & "'"

        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "IBCARIP")
        If ds.Tables.Count > 0 AndAlso ds.Tables("IBCARIP").Rows.Count >0 Then
            lblSAI.Text = ds.Tables("IBCARIP").Rows(inc).Item("SName") & ds.Tables("IBCARIP").Rows(inc).Item("FName")
            lblRate.Text = ds.Tables("IBCARIP").Rows(inc).Item("NRate")
            lblOT.Text = ds.Tables("IBCARIP").Rows(inc).Item("OTRate")
            lblBnk.Text = ds.Tables("IBCARIP").Rows(inc).Item("BName") & ".." &                 ds.Tables("IBCARIP").Rows(inc).Item("ANumber") & ".." & ds.Tables("IBCARIP").Rows(inc).Item("AType")
            con.Close()
            ds.Tables("IBCARIP").DataSet.Clear()
            MaxRows = ds.Tables("IBCARIP").Rows.Count
            'inc = 0
        End if  
    End If
End Sub

答案 1 :(得分:0)

首先,最重要的是:您对SQL-Injection是开放的,因为您没有使用sql参数,而是将查询与用户输入连接。

错误的原因是您尝试访问DataRow中的DataTable而未检查是否至少有一个inc。但是您正在访问索引为da.Fill(ds, "IBCARIP") If ds.Tables("IBCARIP").Rows.Count = 0 Then Return ' or something else ' here you can safely access the first row... 的行,也许该表不包含这么多行。为什么你在这里使用变量?

Using con = New OleDbConnection(dbProvider & dbSource)
    Dim sql = "select * from Calculator where EmpCode=?"
    Using da = New OleDbDataAdapter(sql, con)
        da.SelectCommand.Parameters.AddWithValue("@EmpCode", txtID.Text)
        da.Fill(ds, "IBCARIP")
        If ds.Tables("").Rows.Count > 0 Then
            Dim row = ds.Tables("IBCARIP").Rows(0)
            Dim SName = row.Field(Of String)("SName")
            Dim FName = row.Field(Of String)("FName")
            Dim sai = String.Format("{0}{1}", SName, FName)
            lblSAI.Text = sai
            ' ... '
        End If
    End Using
End Using

这里是带参数的长版本:

{{1}}