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.