Gold
Gold

Reputation: 62554

Error when inserting a record into MS Access

I have val MyDate in my C# program that contain today-date or null.

I have date field in my access 2007 - TdateOpen

I try to insert to the database like this:

SQL = "insert into MyCount(TdateOpen) values ('" + MyDate +"')";

and I get this error:

Data type mismatch in criteria expression

what can be the problem?

Upvotes: 0

Views: 2329

Answers (3)

Johann Blais
Johann Blais

Reputation: 9469

You could SQL parameters instead of dynamically embedding the date value into the statement.

SQL = "insert into MyCount(TdateOpen) values (?)";
var parameter = yourCommand.CreateParameter();
parameter.Value = yourDateTime;
yourCommand.Parameters.Add(parameter);

(DISCLAIMER: The code was not compiled nor tested, but it should give you a hint)

Upvotes: 0

Fionnuala
Fionnuala

Reputation: 91376

You will need to ensure that the date is in US order (mm/dd/yyyy) or ANSI/ISO order, whether you use dash or slash is not important, ANSI/ISO is to be preferred.

Then as, Madhu CM said, the delimiter for dates in Access is hash (#), however, your date can be null and null cannot be delimited, so you will either have to add the delimiter to a date string, if a date is returned, or use two sql statements, one for null and one for date.

Upvotes: 0

Madhu CM
Madhu CM

Reputation: 2556

Coz in your SQL statement you are entering date as String . Instead of String it should be a date/date format. Try to surround by # .

Upvotes: 1

Related Questions