Search code examples
c#oledbjet

Inserting Records Access MDB with OleDB


Why does this happen? :-S Short and simple this time round. Oh, I'm connecting to an MDB file (2007 Access).

Code:

public bool insertRecord(int SR_Number, string Serial_Number, string Model_Number, bool Chargeable, bool Priority, bool Rollback)
    {
        string cmdStr = "INSERT INTO Recovery_CD_Orders ([SR_Number], [Serial_Number], [Model_Number], [Chargeable], [Priority], [Agent_placing_order], [Rollback_reqd]) Values (@SR_Number, @Serial_Number, @Model_Number, @Chargeable, @Priority, @Agent_Placing_Order, @Rollback)";

        OleDbCommand cmd = new OleDbCommand(cmdStr, thisCon);

        cmd.Parameters.AddWithValue("SR_Number", SR_Number);
        cmd.Parameters.AddWithValue("Serial_Number", Serial_Number);
        cmd.Parameters.AddWithValue("Model_Number", Model_Number);
        cmd.Parameters.AddWithValue("Agent_Placing_Order", Environment.UserName);
        cmd.Parameters.AddWithValue("Chargeable", Chargeable);
        cmd.Parameters.AddWithValue("Priority", Priority);
        cmd.Parameters.AddWithValue("Rollback", Rollback);

        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
        cmd.Connection.Close();
        cmd.Dispose();
        return true;
    }

Error:

Data type mismatch in criteria expression.

Table Layout:

SR_Number: Number
Serial_Number: Text
Model_Number: Text
Agent_Placing_Order: Text
Chargeable: Yes/No
Priority: Yes/No
Rollback: Yes/No

Solution

  • Hard to tell from what is presented in the question. I suspect the issue is with the AddWithValue type inference. Your best bet is to explicitly declare the types in old school fashion as that will tell access what to use.