0

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

2
  • The issue is most likely that the value you passed for that column is too long for the column definition. Apart from that, NEVER use string concatenation to insert values into SQL code like that. ALWAYS use parameters. Commented Jan 19, 2018 at 2:07
  • Also, Odbc should be your last choice for an ADO.NET provider. If you're using MySQL then, if you can, you should download Connector/Net from the MySQL web site and use that. MySqlClient is a MySQL-specific provider, just as SqlClient is specific to SQL Server. You can also download some MySQL-specific tools for VS that support EF and such. Commented Jan 19, 2018 at 2:09

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.