Data type mismatch in expression criterian. and Data is not saving in access.

Rimmii

New member
Joined
Jun 18, 2012
Messages
1
Programming Experience
Beginner
I am working in charp with access database. I have made a function and passed values. all fields values are correct but it is showing "Data type mismatch in expression criterian". I have one date picker. how to save it in database.

My function is like this:
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=jewellarydb.accdb;");


        public void purchaseProduct(string productname, string challan_no, string challan_date, string bill_no, string bill_date, string Customer)
        {
            try
            {
                if (con.State != ConnectionState.Open)
                {
                    con.Open();
                    OleDbCommand insert = new OleDbCommand("INSERT INTO [PRODUCT_DETAILS](PRODUCT_NAME,CHALLAN_NO,CHALLAN_DATE,BILL_NO,BILL_DATE,CUST_NAME)VALUES('" + productname + "','" + challan_no + "','" + challan_date + "','" + bill_no + "','" + bill_date + "','" + Customer + "')", con);
                    insert.ExecuteNonQuery();
                    MessageBox.Show("Your data has been saved.");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


            finally
            {
                con.Close();
            }
        }



add.purchaseProduct(txtproductname.Text.ToUpper(), txtchallan_no.Text.ToUpper(), txtchallan_date.Value.ToShortDateString(), txtbill_no.Text.ToUpper(), txtbill_date.Value.ToShortDateString(), txtCustomer.Text.ToUpper());

Please help me soon with this where i am wrong. I have to submit my project and also data is also saving in the access.
 
Last edited by a moderator:
Don't use string concatenation to insert values into SQL code. Use parameters. Also, use data of the proper data type. If you're inserting dates and/or times then use DateTimes, not Strings. Follow the Blog link in my signature and check out my post on Parameters In ADO.NET to learn why and how.
 
Back
Top Bottom