Would you please kindly check the following code for errors that give me a 'Data type mismatch in criteria expression' exception? I just can't seem to find the source of the problem...
*record.Date
of nullable DateTime?
type is explicitly casted to DateTime
*record.Date
is set as nullable for other uses in the program. But the record.Date
set for the INSERT operation is retrieved from a DateTimePicker, so a record.Date
value for this method should never be null.
WHERE
AND (in case you're wondering)
From my Access file (Design View):
Thank you!
Here's the AddRecord method. Thanks!
public static int AddRecord(Record record)
{
OleDbConnection connection = LABMeetingRecordsDB.GetConnection();
string insertStatement = "INSERT INTO DocumentInfo " +
"([FileName], [Date], [Subject], [Type]) " +
"VALUES (?, ?, ?, ?)";
try {
OleDbCommand insertCommand = new OleDbCommand(insertStatement, connection);
insertCommand.Parameters.AddWithValue("@FileName", record.FileName);
insertCommand.Parameters.AddWithValue("@Date", (DateTime)record.Date);
insertCommand.Parameters.AddWithValue("@Subject", record.Subject);
insertCommand.Parameters.AddWithValue("@Type", record.getDBType());
connection.Open();
insertCommand.ExecuteNonQuery();
string selectStatement = "SELECT IDENT_CURRENT('DocumentInfo') FROM DocumentInfo";
OleDbCommand selectCommand = new OleDbCommand(selectStatement, connection);
int recordID = Convert.ToInt32(selectCommand.ExecuteScalar());
AddCategory(connection, recordID, record.Category);
return recordID;
} catch (OleDbException ex) {
throw ex;
} finally {
connection.Close();
}
}
record
in your case.. can you show that structure.. also you need to check for or Cast record.DateTime to DBNULL if it's null can you show more code declaration.. are you familiar with ` null-coalescing operator ??` soinsertCommand.Parameters.AddWithValue("@Date", record.Date ?? DBNull.Value);
perhaps would work – Deflation?
use the ` null-coalescing operator` – Deflationrecord.getDBType()
this is not needed because it will not return the value at all in fact I am actually thinking that it would return System.String or error can you let us know if the changes worked for you..? – Deflationpublic static DateTime? Date { get; set; }
tell me what the value of `Date will be when I run past the line of code..? you are not understanding how to initialize and assign nullable variables you need to assign record.Date – Deflationrecord.Date = dtpDate.Value;
.record.Date
is set asDateTime?
because the nullable type is needed somewhere else, but here, since aRecord
is set with its date set using a DateTimePicker's value, it should never be null. – Udalerecord.Date
is correctly set here. I removed the casting but it still doesn't work. Maybe it has nothing to do withrecord.Date
? – UdaleOleDbCommand
Command type defined try adding this after you declare inserCommandinsertCommand.CommandType = CommandType.Text;
– DeflationOleDbCommand selectCommand = new OleDbCommand(selectStatement, connection);
– Deflation