C # Access OleDb Data Type Mismatch in Criteria Expression

Could you kindly check the following code for errors that give me a 'data type mismatch in the expression of the exception criteria ? I just can't find the source of the problem ...

enter image description here

* record.Datetype nullable is DateTime?explicitly placed inDateTime

* record.DateSet to NULL for other applications in the program. But the set record.Datefor the INSERT operation is retrieved from the DateTimePicker, so the value record.Datefor this method should never be null .

Where

enter image description here

And (in case you are interested)

enter image description here


In my access file (Design View):

enter image description here


Thank!


Here is the AddRecord method. Thank!

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();
        }
    }
+4
3

... [ ]: D

,

OleDbType, , DateTime.Now AddWithValue.

OleDbType, AddWithValue, - DBTimeStamp, Access a OleDbType.Date.

, AddWithValue ...

@LarsTech @DJKraze , , !

+13
OleDbConnection connection = LABMeetingRecordsDB.GetConnection();
string insertStatement = "INSERT INTO DocumentInfo " + "([FileName], [Date], [Subject], [Type]) " + "VALUES (?, ?, ?, ?)";
OleDbCommand insertCommand = new OleDbCommand(insertStatement, connection);
insertCommand.CommandType = CommandType.Text;
insertCommand.Parameters.AddWithValue("@FileName", record.FileName);
insertCommand.Parameters.AddWithValue("@Date", (DateTime)record.Date ?? (object)DBNull.Value);
insertCommand.Parameters.AddWithValue("@Subject", record.Subject);
insertCommand.Parameters.AddWithValue("@Type", record.getDBType ?? (object)DBNull.Value);
connection.Open();
try
{
  insertCommand.ExecuteNonQuery();
}
catch(OleDbException e)
{
   LogYourMessage(e.Message);
}

DBNull.Value () DBNull.Value; . , , .

0

I think you can also use ToString () so that your date data is in the correct format that will be accepted by access

 insertCommand.Parameters.AddWithValue("@Date", record.Date.ToString("dd-MM-yy"));
0
source

All Articles