2013-08-23 46 views
0
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我收到错误“没有一行位0”,下面的代码

输入了错误或不存在的员工代码,我怎么能解决问题的全髋关节置换的消息出现

回答

0

首先最重要的是:由于您没有使用sql参数,而是将查询与用户输入串联在一起,因此您已开放SQL注入。

错误的原因是您正在尝试访问DataTable中的DataRow而不检查是否至少有一个。但是您正在访问索引为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 
+0

蒂姆我会尝试你的代码在不同的形式和表,但它看起来先进的我的水平,,感谢yoiu的帮助,虽然 –

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 
+0

梦幻般的队友它的工作 –

相关问题