5
votes

I'm trying to do an insert in oledb(ms access database) the field called objectdate is date/time

the code i use to add the parameter is this, but i'm getting error.

  OleDbParameter objectdate = new OleDbParameter("@objectdate", OleDbType.DBDate);
  objectdate.Value = DateTime.Now; cmd.Parameters.Add(objectdate);

the error:

Data type mismatch in criteria expression.

3
Please don't put " c#" at the end of your title. On Stack Overflow, we use tags for that.John Saunders
I just copied and pasted those exact statements into C# (VS 2010) and they worked fine for me. Voting to close as "off-topic (...problem that can no longer be reproduced...)".Gord Thompson

3 Answers

10
votes

OleDB doesn't like milliseconds in the datetime parameters. If you remove the milliseconds it will go ok. See also: How to truncate milliseconds off of a .NET DateTime.

0
votes

You could use.

   OleDbParameter objectdate = new OleDbParameter("@objectdate", DbType.DateTime);
   objectdate.Value = DateTime.Now; cmd.Parameters.Add(objectdate);

or use the Ole Automation version of the date.

OleDbParameter objectdate = new OleDbParameter("@objectdate", DbType.DateTime);
       objectdate.Value = DateTime.Now.ToOADate(); cmd.Parameters.Add(objectdate);

Or you could enter the datetime as a literal since the Datetime.ToString() removes the milliseconds that access can't work with.

cmd.Parameters.AddWithValue("@objectdate", DateTime.Now.ToString());

this should work.

0
votes

The sentence:

OleDbParameter objectdate = new OleDbParameter("@objectdate", DbType.DateTime);

is not acepted in visual basic 2008, I use like this:

ordeen.Parameters.Add(New OleDb.OleDbParameter("objectdate", DbType.DateTime))
ordeen.Parameters("objectdate").Value=object.text   'but its not run

the next sentence only functional in sqlserver:

cmd.Parameters.AddWithValue("@objectdate", DateTime.Now.ToString());

the problem in Access continued yet