6

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.

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
robert
  • 321
  • 2
  • 9
  • 20
  • Please don't put " c#" at the end of your title. On [so], we use tags for that. – John Saunders Sep 23 '11 at 03:34
  • 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 Jun 30 '14 at 10:33

4 Answers4

10

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.

Mike Lowery
  • 2,630
  • 4
  • 34
  • 44
Wout
  • 624
  • 5
  • 14
0

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

Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
0

When using OleDb in .netstandard2.0 you can add using the .AddWithValue with just a key value pair. The type is inferred from the value object:

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

Do not convert to string because that would destroy the ability to infer type.

HackSlash
  • 4,944
  • 2
  • 18
  • 44
0

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.

scartag
  • 17,548
  • 3
  • 48
  • 52
  • doesn't work, it say Failed to convert parameter value from a DbType to a DateTime. – robert Sep 23 '11 at 01:26
  • @robert .. could you post the code you used? just in the comments here. – scartag Sep 23 '11 at 01:34
  • @robert i wanted you to replace OleDbType.DBDate with DbType.DateTime – scartag Sep 23 '11 at 01:35
  • the code and the error I'm getting: DateTime dateobjectdate = DateTime.Now; OleDbParameter objectdate = new OleDbParameter("@objectdate", DbType.DateTime); objectdate.Value = dateobjectdate; cmd.Parameters.Add(objectdate); Data type mismatch in criteria expression. – robert Sep 23 '11 at 03:08
  • Also i've tried the next lines with no luck, btw how can indent?? __________________________________________________________________ OleDbParameter objectdate = new OleDbParameter("@objectdate", OleDbType.Date); objectdate.Value = DateTime.Now; cmd.Parameters.Add(objectdate); ________________________________ Data type mismatch in criteria expression. OleDbParameter objectdate = new OleDbParameter("@objectdate", DbType.DateTime); objectdate.Value = DateTime.Now.ToString(); cmd.Parameters.Add(objectdate); Data type mismatch in criteria expression. – robert Sep 23 '11 at 03:22
  • @robert this seems to be a known issue. could you try using DbType.Date ? just to see if it works. – scartag Sep 23 '11 at 03:22
  • @robert i've added other samples that should work. please try them out. – scartag Sep 23 '11 at 03:33
  • i tested the 3 method, and still i'm getting the: Data type mismatch in criteria expression. the access field is datetime, i don't know why it doesn't work – robert Sep 23 '11 at 03:50
  • @robert why don't you just call the Date() function in your sql? access understands that. – scartag Sep 23 '11 at 03:51