1
Answer

Error with access DateTime

russell jones

russell jones

18y
2k
1

Hi there.

This is my first time using MS access from c# and it doesn't seem to work as i would expect (have used sqlserver from c# and vb and access from vb in the past)

I'm inserting a record that includes a date and i keep getting this error Data type mismatch in criteria expression First try:

DateTime DateToAdd = DateTime.Now;
OleDbConnection conn = new OleDbConnection(_DBConnectionString);
OleDbCommand command = new OleDbCommand();
command.Connection = conn;
 
command.CommandText = "INSERT INTO TheTable (DateField) VALUES (@Date)";
command.Parameters.AddWithValue("@Date",DateToAdd);


conn.Open();
command.ExecuteNonQuery();

So i thought maybe i need to set the type on my Parameter:

DateTime DateToAdd = DateTime.Now;
OleDbConnection conn = new OleDbConnection(_DBConnectionString);
OleDbCommand command = new OleDbCommand();
command.Connection = conn;
 
command.CommandText = "INSERT INTO TheTable (DateField) VALUES (@Date)";
OleDbParameter newParam = new OleDbParameter("@Date", OleDbType.Date);
newParam.Value = DateToAdd;

command.Parameters.Add(newParam);

conn.Open();
command.ExecuteNonQuery();

I assumed that oleDB would be able to convert the date OK but it seems to have problems, does anyone know what i'm doing wrong?

Thanks in advance Russell

Answers (1)