Click here to Skip to main content
15,913,090 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i try to execute the following code, i get an error message saying data type mismatch in criteria expression. I am using MS Access database

What I have tried:

Here's the code

C#
private void btnSave_Click(object sender, EventArgs e)

        {
            try
            {
                conn.Open();

                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = conn;
                cmd.CommandText = "insert into master (MOW,GK,CLASS,MAKE,MODEL,YOP,MNTH,PRICE,ENGINE_NUMBER,CHASSIS_NUMBER,CC,HP,LOCATION,LOGBOOK,MERCHANT,REPLACED_ENGINE_NUMBER,REMARKS,COUNTY) values('" + txtMOW.Text + "','" + txtGK.Text + "','" + txtCLASS.Text + "','" + txtMAKE.Text + "','" + txtMODEL.Text+ "','" + txtYOP.Text + "','" + txtMNTH.Text + "','" + txtPRICE.Text + "','" + txtENGINENUMBER.Text + "','" + txtCHASSISNUMBER.Text + "','" + txtCC.Text + "','" + txtHP.Text + "','" + txtLOCATION.Text + "','" + txtLOGBOOK.Text + "','" + txtMERCHANT.Text + "','" + txtRENGINENUMBER.Text + "','" + txtREMARKS.Text + "','" + txtCOUNTY.Text + "')";

                cmd.ExecuteNonQuery();
                MessageBox.Show("Data Saved !!!!");
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex.Message);
            }

        }
Posted
Updated 18-Mar-16 0:11am
v2
Comments
Michael_Davies 18-Mar-16 6:05am    
Hi, without knowing what is in the text boxes and what the schema is for the table it is difficult to help.

Recommend you do not construct an SQL string, use parameters instead and you will get to see the actual field in error as you add the parameter values and it fails.

If you must construct an SQL string, use the debugger to look at it's contents before you execute it which might help spot the problem.
[no name] 18-Mar-16 6:07am    
This is pretty much straight forward. Just cross datatype of all the columns which are using in your query. Because you are passing value as character/string format, if some column is integer then will throw exception(what is you are getting now).
Sarrrva 18-Mar-16 6:13am    
Can you provide that table schema ?

1 solution

Don't do it like that.
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
The chances are that your problem will disappear at the same time...
For example:
C#
using (OleDbCommand cmd = new OleDbCommand("INSERT INTO master (Col1, Col2) VALUES (?,?)", conn))
   {
   cmd.Parameters.AddWithValue("@COL1", valueForColumn1);
   cmd.Parameters.AddWithValue("@COL2", valueForColumm2);
   cmd.ExecuteNonQuery();
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900