Search code examples
c#oledbdbfvisual-foxpro

OleDbException was unhandled - Data type mismatch for Int-Field Type


I have a free table My_Table1. My_Table1 ia having the following field

Field-Name  ->  sl_no Integer - Width 4

Therefore from c# while I insert through OleDb its' giving the error like

"Data Type Mismatch"

And My OleDbCommand is

int a1 = 3;

OleDbCommand OleCmd1 = new System.Data.OleDb.OleDbCommand("Insert into My_Table1 values('"+a1+"')", OleCon1)

Thanks For The Helps


Solution

  • The INSERT statement is inserting a character value 'a1'. That cannot be stored in an integer field. If a1 is a variable in the code, then maybe you are looking for this:

    System.Data.OleDb.OleDbCommand("Insert into My_Table1 values("+a1.ToString()+")", OleCon1)
    

    From a "best practices" perspective, it is probably better to use a parameterized query just to get in the habit of avoiding SQL injection attack problems. With an integer value, the above statement is probably okay, but it is not generally good to get comfortable with building SQL statements with simple concatenation.