0

I have in my Ms-Access database table, a date field in Short Date format.

I have in my C# program DateTimePicker control in dd/MM/yyyy short date format

I try to insert data using C# code like this:

SQL = "insert into MyTbl(D_from,D_to) values (@MyFrom,@MyTo)";
OleDbCommand Cmd = Conn.CreateCommand();
OleDbParameter dateparam1 = Cmd.Parameters.AddWithValue("@MyFrom", DbType.DateTime);
dateparam1.Value = dt_From.Value; 

OleDbParameter dateparam2 = Cmd.Parameters.AddWithValue("@MyTo", DbType.DateTime);
dateparam2.Value = dt_To.Value;

Cmd.CommandText = SQL;
Cmd.ExecuteNonQuery();

and I got the error: Data type mismatch in criteria expression.

3 Answers 3

2

change Parameters.AddWithValue to Parameters.Add

Cmd.Parameters.Add("@MyFrom", DbType.DateTime);

if you use Parameters.AddWithValue then you need to pass the Value as Second Parameter, not the DataType

Cmd.Parameters.AddWithValue("@MyFrom", dt_From.Value);

and also you need to set the CommandType as Text

Sign up to request clarification or add additional context in comments.

1 Comment

OleDbCommand Cmd = Conn.CreateCommand(); Cmd.Parameters.AddWithValue("@MyFrom", dt_From.Value); Cmd.Parameters.AddWithValue("@MyTo", dt_To.Value); Cmd.CommandText = SQL; Cmd.ExecuteNonQuery();
0

Take a look at this question

Data type mismatch in criteria expression | Access, OleDb, C#

Comments

0

Try this

dateparam1.Value = dt_From.Value.ToShortDateString();
dateparam2.Value = dt_To.Value.ToShortDateString();

Otherwise have look at this :

Date values must be either delimited according to the ODBC canonical date format or delimited by the datetime delimiter ("#"). Otherwise, Microsoft Access will treat the value as an arithmetic expression and will not raise a warning or error.

For example, the date "March 5, 1996" must be represented as {d '1996-03-05'} or #03/05/1996#; otherwise, if only 03/05/1993 is submitted, Microsoft Access will evaluate this as 3 divided by 5 divided by 1996. This value rounds up to the integer 0, and since the zero day maps to 1899-12-31, this is the date used.

A pipe character (|) cannot be used in a date value, even if enclosed in back quotes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.