0

I was struggling so much with something that I eventually found out how to fix, but I don't know the underlying cause. If I add a DateTimePicker (in this case, dtRecordBatchDate) to a form and the user doesn't change it's default date and time and I pass its DateTimePicker.Value to a SQL command using Parameters property, I get this error:

using (OleDbCommand command = new OleDbCommand("INSERT INTO Herbs_Stock (herb_id,batch_number,batch_date,quantity,cost,shipcost) VALUES (?,?,?,?,?,?)", Program.myconnection))
{
     command.Parameters.AddWithValue("@herb_id", herb_id);
     command.Parameters.AddWithValue("@batchnumber", batch_number);

     command.Parameters.AddWithValue("@batch_date", dtRecordBatchDate.Value); // HERE'S THE IMPORTANT PART

     command.Parameters.AddWithValue("@quantity", quantity);
     command.Parameters.AddWithValue("@cost", cost);
     command.Parameters.AddWithValue("@shipcost", shipcost);
     command.ExecuteNonQuery();
}

Data Type Mismatch in Criteria Expression

But if I add only one line before passing to the command it will be fixed: dtRecordBatchDate.Value=DateTime.Today.

Why is it like so? This is weird because type of DateTimePicker.Value is DateTime not DateTime? and it's got already defaulted to DateTime.Now (or a few seconds before :)). I'm very curious about this.

Ehsan88
  • 3,569
  • 5
  • 29
  • 52
  • What is the type of your `batch_date` column? What is the value of `dtRecordBatchDate.Value.Date` when you look in debugger? Why don't you use just `dtRecordBatchDate.Value` instead? – Soner Gönül Nov 28 '13 at 12:30
  • [_Criteria Mismatch_ usually means that the data you are trying to put into the database can't be accepted because the database is expecting a different type of data.](http://stackoverflow.com/a/16297461/447156) – Soner Gönül Nov 28 '13 at 12:31
  • I have looked at all these things! they are all alright. Column is of type `DateTime` and so are `dtRecordBatchDate.Value` and `dtRecordBatchDate.Value.Date`. I think the issue is more complicated and perhaps it's a bug. – Ehsan88 Nov 28 '13 at 15:02

0 Answers0