lhander
lhander

Reputation: 9

MySQL Query Browser Error Data truncated for column

This is my code and I dont know why I'm getting this Error in my visual studio 2013 and my data base is MySQL Query Browser:

"Additional information: ERROR [HY000] [MySQL][ODBC 3.51 Driver][mysqld-5.1.34-community]Data truncated for column 'userid' at row 1"

If a = "New" Then Dim sqlstring As String sqlstring = "INSERT into users(username, userid, usertype, remarks)values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & ComboBox1.Text & "','" & TextBox4.Text & "')" cmd = New Odbc.OdbcCommand(sqlstring, cnn) cmd.ExecuteNonQuery() reset() disable() btn3() End If

Upvotes: 0

Views: 607

Answers (1)

John Woo
John Woo

Reputation: 263933

This is because the total length of the characters you are passing exceeded the length defined by column userid. Aside from that, mysql has its own managed provider called MySqlClient and this is the one you should be using.

A much better way to good practice programming is to paramaterized your query. Example below is at least your good starting point:

Dim connectionString As String = "..your connection string here..."
Using SQLConnection As New MySqlConnection(connectionString)
   Using sqlCommand As New MySqlCommand()
      sqlCommand.CommandText = "INSERT into users(username, userid, usertype, remarks) VALUES (@username, @userid, @usertype, @remarks)"
      sqlCommand.Connection = SQLConnection
      sqlCommand.CommandType = CommandType.Text
      sqlCommand.Parameters.AddWithValue("@username", TextBox1.Text)
      sqlCommand.Parameters.AddWithValue("@userid", TextBox2.Text)
      sqlCommand.Parameters.AddWithValue("@usertype", ComboBox1.Text)
      sqlCommand.Parameters.AddWithValue("@remarks", TextBox4.Text)
      SQLConnection.Open()
      sqlCommand.ExecuteNonQuery()
   End Using
End Using

Upvotes: 0

Related Questions