2013-08-23 43 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

夢幻般的隊友它的工作 –

相關問題