Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Data type mismatch in criteria expression.

Hi Everyone,

I'm currently building an application that acts as a front-end to a MS Access database. I've put all of my queries into Access and run them from my VB program as stored procedures. I've found on some forms I will get a "System.Data.OleDb.OleDbException was unhandled" error. This happens only if I do not change the value in the DateTimePicker I have on the form. If I do change the value to a date other than today, the procedure runs fine. Here is the code in VB and the SQL statement from Access. Thanks for any help!

'Initialize a new instance of the OleDbConnection Class
objConnection = New OleDbConnection(strConnectionString)
'Initialize a new instance of the OleDbCommand class
objCommand = New OleDbCommand
'Set the objCommand object properties
Try
objConnection.Open()
Catch OleDbExceptionErr As OleDbException
MessageBox.Show(OleDbExceptionErr.Message, "Container Opening")
Catch InvalidOperationExceptionErr As InvalidOperationException
MessageBox.Show(InvalidOperationExceptionErr.Message, "Container Opening")
End Try

With objCommand
.CommandText = "usp_UpdateOpen"
.CommandType = CommandType.StoredProcedure
.Connection = objConnection
End With

'Add the required parameters for the query
objCommand.Parameters.Add("@SealNumber", OleDbType.Char).Value = txtSealNumber.Text
objCommand.Parameters.Add("@SealLocation", OleDbType.Char).Value = strSealLocation
objCommand.Parameters.Add("@ContainerOpened", OleDbType.DBTimeStamp).Value = dtpOpened.Value
objCommand.Parameters.Add("@ContainerLocation", OleDbType.Char).Value = strContainerLocation
objCommand.Parameters.Add("@ContainerStatus", OleDbType.Char).Value = "Airing"
objCommand.Parameters.Add("@ExamID", OleDbType.Char).Value = cbxFileNumber.Text

objCommand.ExecuteReader()

'Clean up
objCommand.Dispose()
objCommand = Nothing
objConnection.Close()
objConnection.Dispose()
objConnection = Nothing

UPDATE Exams SET SealNumber = [@SealNumber], SealLocation = [@SealLocation], ContainerOpened = [@ContainerOpened], ContainerLocation = [@ContainerLocation], ContainerStatus = [@ContainerStatus]
WHERE ExamID=[@ExamID];
[2563 byte] By [scott0_1] at [2007-11-11 10:31:06]
# 1 Re: Data type mismatch in criteria expression.
The parameter data type for an Access date value should be:

System.Data.OleDb.OleDbType.Date

Give that a try to see if it fixes the problem.
pclement at 2007-11-11 20:47:31 >
# 2 Re: Data type mismatch in criteria expression.
Seemed almost too easy! Thanks for the help.

:WAVE:
scott0_1 at 2007-11-11 20:48:37 >