0

I try insert datetime value to access database but always get me error;

my source code :

cmd.CommandText = "INSERT INTO tblProcessed (name, comment, file_name, seven_zip_name, mode, device_ID,send_date) VALUES(?,?,?,?,?,?,?)";
cmd.Parameters.AddWithValue("", name);
cmd.Parameters.AddWithValue("", comment);
cmd.Parameters.AddWithValue("", Filename);
cmd.Parameters.AddWithValue("", sevenZip);
cmd.Parameters.AddWithValue("", mode);
cmd.Parameters.AddWithValue("", iddev);
cmd.Parameters.AddWithValue("",dati); //when i remove this line wont get any error
cmd.ExecuteNonQuery();

and always get me error :

[Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression."}

All field type is string except send_date field ... when I remove send_date field every thing work correctly

How I insert datetime value to send_date field ?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
abbas-h
  • 392
  • 1
  • 4
  • 18
  • http://stackoverflow.com/questions/18082840/how-to-bind-parameters-via-odbc-c – IVAAAN123 Sep 19 '14 at 04:45
  • i change to ` cmd.Parameters.Add("", OdbcType.NVarChar).Value = name; cmd.Parameters.Add("", OdbcType.NVarChar).Value = comment; cmd.Parameters.Add("", OdbcType.NVarChar).Value = Filename; cmd.Parameters.Add("", OdbcType.NVarChar).Value = sevenZip; cmd.Parameters.Add("", OdbcType.NVarChar).Value = mode; cmd.Parameters.Add("", OdbcType.NVarChar).Value = iddev; cmd.Parameters.Add("", OdbcType.DateTime).Value = dati; ` not wotk – abbas-h Sep 19 '14 at 05:02
  • what about parameter names? – IVAAAN123 Sep 19 '14 at 05:14
  • What are parameters values for this query? – IVAAAN123 Sep 19 '14 at 05:21
  • all values is string except send_date is DateTime – abbas-h Sep 19 '14 at 08:58
  • What is the value of `dati` exactly? And don't use `AddWithValue` anymore. http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ – Soner Gönül Sep 22 '14 at 11:29

1 Answers1

1

Ok, I ran into the same problem and this is how I solved it.

I was using the "AddWithValue" and I could only get it to add a straight date, but I wanted the time portion also.

Try

cmd.Parameters.Add("@send_date", OleDbType.DBTimeStamp).Value = DateTime.Parse(dati.ToString());

From what I read elsewhere, the add is having problems with the milliseconds part of the date, and it looks like this method must strip them off.

Oldtimer
  • 26
  • 1