Click here to Skip to main content
15,889,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What's wrong in my code? :( it gives an error. Datatype mismatch in criteria expression.

VB
Dim dd = Date.Today.Date
       Dim shar As String = "Overdue"
       Connect()
       SQLQuery = "UPDATE  tblBookBorrowed SET status = '" & shar & "' WHERE " & dd & " > datereturn AND penaltyyesno = 'True'"
       cmd = New OleDb.OleDbCommand(SQLQuery, conn)
       cmd.ExecuteNonQuery()

       conn.Dispose()
       conn.Close()
       cmd = Nothing
Posted
Updated 18-Feb-18 19:59pm
Comments
[no name] 22-Jul-12 16:23pm    
I would imagine that it's just was it says. You have a data type mismatch.
db7uk 22-Jul-12 17:08pm    
I think it is something to do with the date (dd). Try casting it or formatting the date in the correct manor that your database uses?

Why is your True value in a string ? Are you storing a string here or a bool ? I think you need to remove those quotes.
 
Share this answer
 
I think db7uk is correct, the error is in the date field. The SQLQuery string should look like this:

VB
SQLQuery = "UPDATE  tblBookBorrowed SET status = '" & shar & "' WHERE #" & String.Format("{0:d}", dd) & "# > datereturn AND penaltyyesno = 'True'"


With using String.Format("{0:d}", dd) you convert the date to a short date based on your local country and language settings. I tested this on my nl-NL computer, it worked but I expected that I had to use en-US (07\24\2012 in stead of the dutch 24-07-2012). When you have other locales I guess it might work. Using paramers would solve this problem.

The issue about formatting parameters in a command might be better solved by using parameters. You code would look something like this:

VB
Dim dd = Date.Today.Date
Dim shar As String = "Overdue"
Connect()
SQLQuery = "UPDATE  tblBookBorrowed SET status = ? WHERE ? > datereturn AND penaltyyesno = ?"
cmd = New OleDb.OleDbCommand(SQLQuery, conn)

'Parameters are defined based on their sequential order in the SQL statement
cmd.Parameters.Add("Status", OleDb.OleDbType.VarChar, 50) 'The last parameter is the size, adjust to actual size
cmd.Parameters.Add("Returndate", OleDb.OleDbType.Date)
cmd.Parameters.Add("Penalty", OleDb.OleDbType.Boolean)

cmd.Parameters("Status").Value = shar
cmd.Parameters("Returndate").Value = dd
cmd.Parameters("Penalty").Value = True

cmd.ExecuteNonQuery()

conn.Dispose()
conn.Close()
cmd = Nothing


I hope this is of use
 
Share this answer
 
hi,

I am getting error while update the values.This shows the error data type mismatch in criteria expression,which is failed to update.I am trying with the access database connection.

this is my code

Try
con = New OleDbConnection(cnString)
con.Open()
cmd1 = New OleDbCommand("Update Faculty Set FName='" & txtfn.Text & "', MName='" & txtmn.Text & " ', LName='" & txtln.Text & " ',Address='" & txtadd.Text & "',Contact='" & txtcontact.Text & "',Gender='" & r & "',OnService='" & r1 & "' where FacultyID=" & Integer.Parse(Label15.Text) & "", con)
cmd1.ExecuteNonQuery()
MessageBox.Show("Sucessfully Updated!!")
clear()
Display()
Catch ex As Exception
MessageBox.Show("Property Failed to Update!!", ex.Message)
End Try
 
Share this answer
 
Sub display()
Text1.Text = rs!Name
Text2.Text = rs!Rollno
Text3.Text = rs!Branch
Text4.Text = rs!Class
Text5.Text = rs!Address
Text6.Text = rs!Contactno
End Sub

Private Sub gosearch_Click()
rs.Close
rs.Open "Select * from studdata where( Rollno='" & Text7.Text & "')", con, adOpenDynamic, adLockPessimistic
If Not rs.EOF Then
display
reload
Else
MsgBox "Record not found", vbInformation
End If
End Sub

Sub reload()
rs.Close
rs.Open "Select * from studdata", con, adOpenDynamic, adLockPessimistic
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900