2014-05-21 67 views
0

每次嘗試向我的數據庫表中添加信息時,都會產生錯誤。將信息添加到數據庫時出現System.ArgumentException

Error: An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll 

Additional information: Input string was not in a correct format.Couldn't store <JHK> in KEY TAG Column. Expected type is Int32. 

這是我的代碼到目前爲止。

Imports System 
Imports System.Data 
Imports System.Data.OleDb 

Public Class add_new_key_window 

Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) 

End Sub 

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
    Me.Close() 
End Sub 

Private Sub add_new_key_window_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

End Sub 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    'saveRecord() 
    management_window.RKM_SystemTableAdapter.Insert(Me.TextBox1.Text, Me.TextBox2.Text, Me.TextBox3.Text, Me.TextBox4.Text, Me.TextBox5.Text, Me.TextBox6.Text, Me.RichTextBox1.Text) 
    management_window.RKM_SystemTableAdapter.Fill(management_window.RKM_System_dataDataSet.RKM_System) 
    clearTextboxes() 
End Sub 

'Public Sub saveRecord() 
' Dim cn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data source=" & Application.StartupPath & "\RKM System data.accdb") 
' Dim str As String 

' Try 
'  cn.Open() 
'  Dim cmd As New System.Data.OleDb.OleDbCommand("INSERT INTO [RKM System]([KEY TAG], LOCATION, [HOOK NO], [KEY TYPE], BRAND, SERIAL, [TAG COLOR], NOTES) " & _ 
'              "VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "','1','" & TextBox3.Text & _ 
'              "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & _ 
'              "','" & RichTextBox1.Text & "') ", cn) 
'  str = cmd.ExecuteNonQuery 
'  MsgBox("The new record was inserted successfully!") 

' Catch ex As Exception 
'  MsgBox("Error message: " & ex.Message) 
' End Try 
' cn.Close() 
'End Sub 

Public Sub clearTextboxes() 
    TextBox1.Clear() 
    TextBox2.Clear() 
    TextBox3.Clear() 
    TextBox4.Clear() 
    TextBox5.Clear() 
    TextBox6.Clear() 
    RichTextBox1.Clear() 
End Sub 

End Class 

回答

0

您需要的值轉換在textbox1.text爲整數 - 目前它特林字符串存儲在一個Int32場

例如Access數據庫可以有一個字段的類型爲「數字」,你必須將其中的某種數字。您試圖直接將文本框的內容放入不是數字的字段中,它是一個字符串。因此,您需要使用「int.Parse(textbox1.Text)」或類似方法將文本框中的數據「轉換」爲正確的類型。

順便說一句,你應該從不使用內聯語句創建這樣的SQL查詢時。使用SQL參數,否則你打開自己的潛在的SQL注入攻擊。

例如(在C#不算太出色了我的VB!):

var cmd = new System.Data.OleDb.OleDbCommand("INSERT INTO table1 (firstname, lastname) VALUES (@firstname, @lastname)"); 

cmd.Parameters.AddWithValue("@id", (int)textbox1.text); 
cmd.Parameters.AddWithValue("@firstname", textbox2.text); 

欲瞭解更多信息請參閱

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

+1

Int.TryParse是你的朋友... –

+0

冬青牛...我忘了提及...我是一個非常初學者。我知道需要解釋很多,但我對編碼充滿激情,我想學習。有沒有解釋的機會,你會向初學者解釋?非常感謝你快速回來。 – Kobra

+0

在關於語法錯誤的問題流向你之前,你可能想現在把它轉換成VB。另外,您要添加兩次TB1,而OleDb實際上並不使用可能會引起誤解的命名參數。 – Plutonix

0
Public Sub saveRecord() 
    Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data source=" & 
       myDbFileName) 

    Dim SQL as String = "INSERT INTO [RKM System]([KEY TAG], LOCATION, [HOOK NO], 
      [KEY TYPE], BRAND, SERIAL, [TAG COLOR], NOTES) 
      VALUES (?, ?, ?, ?, ?, ?, ?, ?)" 
      ' ? = a place holder for a parameter to be added later 

    Try 
     cn.Open() 

     Using cmd As New OleDBCommand(SQL, cn) 
      ' add param values IN ORDER SPECIFIED 
      cmd.Parameters.AddWithValue("@p1", Convert.ToInt32(TextBox1.Text)) 
      cmd.Parameters.AddWithValue("@p2", TextBox2.Text) 
      ' etc 
      ' number of AddWithValue lines must = number of "?" in SQL 

      cmd.ExecuteNonQuery 
     End Using   ' disposes of the OLEDbCOmmand object 


     MessageBox.Show("The new record was inserted successfully!") 

    Catch ex As Exception 
     MessageBox.Show("Error message: " & ex.Message) 
    End Try 
    cn.Close() 
End Sub 

參數避免SQL注入攻擊(閱讀了關於這個)。由於[KEY TAG]是數據庫中的一個整數列,而TextBox1.Text是字符串(總是),因此您需要使用Convert.ToInt32來轉換該值(因爲它是用戶輸入,所以在實際情況下應實際使用Integer.TryParse他們進入「紅色」或「Ziggy」)。

命名參數的簡單佔位OLEDB對待,所以這可以給你帶來麻煩:

Dim SQL As String = "INSERT INTO Employee (FirstName, LastName) 
      Values (@FName, @LName)" 

     cmd.Parameters.AddWithValue("@LName", TextBox1.Text) 
     cmd.Parameters.AddWithValue("@FName", TextBox2.Text) 

TextBox1的將映射到名字,因爲它是在SQL第一列名。

相關問題