Data type mismatch

Tinten Fruelda

New member
Joined
Jul 31, 2011
Messages
2
Location
Valenzuela City, Philippines
Programming Experience
Beginner
I'm trying to insert a new record in my database but my query can't execute because of this error "Data type mismatch in criteria expression" although I'm basing my code from a book. I'm new to this, so hopefully someone could help me.

This is the code:

Private Sub b1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b1.Click
Try


If (txt1.Text <> "" AndAlso txt2.Text <> "") Then


ReservationsAdapter.InsertCommand.CommandText = _
"INSERT INTO Reservations(FirstName, LastName, Birthdate, ContactNo, Occupation, Address ) " & _
"VALUES( '" & txt2.Text & "' , '" & txt1.Text & "' , '" & txt3.Text & "' , '" & txt4.Text & "' , '" & txt5.Text & "' , '" & txt6.Text & "')"


ReservationsConnection.Open()
MsgBox("Connection Open")



ReservationsAdapter.InsertCommand.ExecuteNonQuery()
MsgBox("Query Executed")


Else
MsgBox("Please fill up the form")
End If


Catch ex As Exception
MsgBox(ex.ToString())


End Try


End Sub
End Class
 
Your code is inserting six text values into the database but I'm guessing that at least one of your columns is some other data type. This is just one of the reasons that you should never use string concatenation to insert variables into SQL code. Follow the Blog link in my signature and check out my post on ADO.NET Parameters to learn the proper way.
 
Back
Top