I have a problem inserting data into a table on a MySQL database using a VB.NET application. I have a simple form where when I set some data to the textboxes and I press a
Dim connString as String ="server=localhost;userid=root;password=123456;database=uni_park_db"
Dim conn as MySqlConnection(connString)
Dim cmd as MysqlCommand
Dim dt as New DataTable
Dim ireturn as Boolean
Private Sub Insert_Car()
Dim sql as String = "insert into members_car (car_id, member_id, model, color, chassis_id, plate_number, code) values (@car_id,@member_id,@model,@color,@chassis_id,@plate_number,@code)"
Dim cmd = new MySqlCommand(sql, conn)
cmd.Paramaters.AddwithValue("@car_id", txtCar.Text)
cmd.Paramaters.AddwithValue("@member_id", txtMember.Text)
cmd.Paramaters.AddwithValue("@model", txtModel.Text)
cmd.Paramaters.AddwithValue("@color", txtColor.Text)
cmd.Paramaters.AddwithValue("@chassis_id", txtChassis.Text)
cmd.Paramaters.AddwithValue("@plate_number", txtPlateNo.Text)
cmd.Paramaters.AddwithValue("@code", txtCode.Text)
Try
conn.Open()
If cmd.ExecuteNonQuery() > 0 Then
ireturn = True
End If
conn.Close()
Catch ex as Exception
ireturn = False
conn.Close()
End Try
Return ireturn
End Sub
You need to use ?param instead of @param when performing queries to MySQL
str_carSql = "insert into members_car (car_id, member_id, model, color, chassis_id, plate_number, code) values (?id,?m_id,?model,?color,?ch_id,?pt_num,?code)"
sqlCommand.Connection = SQLConnection
sqlCommand.CommandText = str_carSql
sqlCommand.Parameters.AddWithValue("?id", TextBox20.Text)
sqlCommand.Parameters.AddWithValue("?m_id", TextBox20.Text)
sqlCommand.Parameters.AddWithValue("?model", TextBox23.Text)
sqlCommand.Parameters.AddWithValue("?color", TextBox24.Text)
sqlCommand.Parameters.AddWithValue("?ch_id", TextBox22.Text)
sqlCommand.Parameters.AddWithValue("?pt_num", TextBox21.Text)
sqlCommand.Parameters.AddWithValue("?code", ComboBox1.SelectedItem)
sqlCommand.ExecuteNonQuery()
Change the catch block to see the actual exception:
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
After instantiating the connection, open it.
SQLConnection = New MySqlConnection()
SQLConnection.ConnectionString = connectionString
SQLConnection.Open()
Also, avoid building SQL statements by just appending strings. It's better if you use parameters, that way you win on performance, your program is not prone to SQL injection attacks and your program is more stable. For example:
str_carSql = "insert into members_car
(car_id, member_id, model, color, chassis_id, plate_number, code)
values
(@id,@m_id,@model,@color,@ch_id,@pt_num,@code)"
And then you do this:
sqlCommand.Parameters.AddWithValue("@id",TextBox20.Text)
sqlCommand.Parameters.AddWithValue("@m_id",TextBox23.Text)
' And so on...
Then you call:
sqlCommand.ExecuteNonQuery()
You need to open the connection first:
SQLConnection.Open();
sqlCommand.CommandType = CommandType.Text
@
and not ?
try this:
Public Function InsertCar() As Boolean
Dim iReturn as boolean
Using SQLConnection As New MySqlConnection(connectionString)
Using sqlCommand As New MySqlCommand()
With sqlCommand
.CommandText = "INSERT INTO members_car (`car_id`, `member_id`, `model`, `color`, `chassis_id`, `plate_number`, `code`) values (@xid,@m_id,@imodel,@icolor,@ch_id,@pt_num,@icode)"
.Connection = SQLConnection
.CommandType = CommandType.Text // You missed this line
.Parameters.AddWithValue("@xid", TextBox20.Text)
.Parameters.AddWithValue("@m_id", TextBox20.Text)
.Parameters.AddWithValue("@imodel", TextBox23.Text)
.Parameters.AddWithValue("@icolor", TextBox24.Text)
.Parameters.AddWithValue("@ch_id", TextBox22.Text)
.Parameters.AddWithValue("@pt_num", TextBox21.Text)
.Parameters.AddWithValue("@icode", ComboBox1.SelectedItem)
End With
Try
SQLConnection.Open()
sqlCommand.ExecuteNonQuery()
iReturn = TRUE
Catch ex As MySqlException
MsgBox ex.Message.ToString
iReturn = False
Finally
SQLConnection.Close()
End Try
End Using
End Using
Return iReturn
End Function
your str_carSql should be exactly like this:
str_carSql = "insert into members_car (car_id, member_id, model, color, chassis_id, plate_number, code) values (@id,@m_id,@model,@color,@ch_id,@pt_num,@code)"
Good Luck