none
Additional information: Data type mismatch in criteria expression. C# Oledb connectioon RRS feed

  • Question

  • I want to insert data into table PO and throws this exception,  this is my code, whats wrong with it.

    f2.oleDbConnection1.Open();
                OleDbCommand cmd = new OleDbCommand("Insert into PO(POID,VName,VID,VContectPerson,VCPPH,PODate,VDept,Status,TotalAmount) values(?,?,?,?,?,?,?,?,?)", f2.oleDbConnection1);
                cmd.Parameters.AddWithValue("@POID", textBox1.Text);
                cmd.Parameters.AddWithValue("@VName", textBoxf.Text);
                cmd.Parameters.AddWithValue("@VID", comboBox2.Text);
                cmd.Parameters.AddWithValue("@VContectPerson", textBoxi.Text);
                cmd.Parameters.AddWithValue("@VCPPH", textBoxh.Text);
                cmd.Parameters.AddWithValue("@PODate", dateTimePicker1.Text);
                cmd.Parameters.AddWithValue("@VDept", comboBox1.Text);
                cmd.Parameters.AddWithValue("@Status", label21.Text);
                cmd.Parameters.AddWithValue("@TotalAmount", textBox3.Text);
                cmd.ExecuteNonQuery();
                f2.oleDbConnection1.Close();

    Saturday, June 23, 2018 2:36 PM

All replies

  • Instead of AddWithValue, use a different method to add parameters that allows you to specify OleDb types that match the data type of the target table. The problem with AddWithValue is that it infers the parameter data type from the provided .NET object and those are all strings here because you are using the Text property value. I can't be specific without knowing the DBMS or table structure but an example is:

       cmd.Parameters.Add("@TotalAmount", Decimal.Parse(textBox3.Text));

    In the case of the DateTimePicker control, use the Value property instead of the Text property as that will be strongly typed.


    Dan Guzman, Data Platform MVP, http://www.dbdelta.com

    • Proposed as answer by BonnieBMVP Sunday, June 24, 2018 4:42 PM
    Saturday, June 23, 2018 3:55 PM
  • thanks Dan, i Used System.DateTime.Today Instead of Date and time picker and it worked.. :) 
    Saturday, June 23, 2018 6:16 PM
  • Hi Prvz,

    I am glad to know that you resolve the issue and thank you for sharing solution to us, please mark it as answer, it will be beneficial to other communities who have the similar issue.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 25, 2018 8:18 AM
    Moderator