I have created a table in mysql database table(tblmysq) with id(autoincrement),name(string),branch(string),sex(string),and dat(datetime).The following error occurs when i try to insert a record:check the manual that corresponds to your mysql server version for the right syntax near line 1.
My lines of code are :
Try
conn = New MySqlConnection(conStr)
conn.Open()
'Dim sql As String = String.Format("INSERT INTO tblmysq(name,branch,sex,dat) VALUES('{0}','{1}','{2}',{3}, TextBox1.Text , TextBox2.Text,ComboBox1.SelectedItem,DateTimePicker1.Value)")
Dim sql As String = "INSERT INTO tblmysq(name,branch,sex,[dat])VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & ComboBox1.SelectedItem & "'," + Format(DateTimePicker1.Value, "yyyy-MM-dd") + ")"
com = New MySqlCommand(sql, conn)
If com.ExecuteNonQuery Then
MessageBox.Show("Save")
Else
MessageBox.Show("Operation not successful")
End If
Catch ex As Exception
MessageBox.Show("Error:" & ex.ToString())
End Try
When i enclose dat in square bracket i.e [dat], the error ends with the value such as:
...you have an error near '[dat] VALUES ('paul','phc','male',2013-11-16)'at line 1.
Thanks for help in anticipation.