Click here to Skip to main content
16,017,922 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I try to save data's from my form to MS Access Database. There are 13 Fields to save but when i click save button error shown on this line
C#
top.ExecuteNonQuery(); Data type mismatch in criteria expression.


This is my code help me as possible
C#
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/Srihari/Srihari/Invoice.accdb");
       conn.Open();

       // to save top column in invoive
       int invoicenumber = Convert.ToInt32(TXE_Invoice_Number.Text);
       string terms = CBL_Terms.Text;
       DateTime date = CBL_Date.DateTime;
       string ourquote = TXE_OurQuote.Text;
       string salesperson = CBL_Sales_Person.Text;
       string customername = CBL_Customer_Nmae.Text;
       string oderno = CBL_Order_Number.Text;
       string invoiceaddress = TXE_Invoice_Address.Text;
       string deliveryaddress = TXE_Delivery_Address.Text;

       bool inclusive = CBX_New.Checked;

       decimal Price = Convert.ToDecimal(TXE_Price.Text);
       decimal tax = Convert.ToDecimal(TXE_Tax.Text);
       decimal grandtotal = Convert.ToDecimal(TXE_Total.Text);

       OleDbCommand top = new OleDbCommand("INSERT INTO test_top(InvoiceNumber,Terms,[InvoiceDate],OurQuote,SalesPerson,CustomerName,OrderNumber,InvoiceAddress,DeliveryAddress,InclusiveStatus,Price,Tax,GrandTotal) VALUES (" + invoicenumber + ",'" + terms + "','" + date + "','" + ourquote + "','" + salesperson + "','" + customername + "','" + oderno + "','" + invoiceaddress + "','" + deliveryaddress + "'," + inclusive + "," + Price + "," + tax + "," + grandtotal + ")", conn);
       top.ExecuteNonQuery();
       MessageBox.Show("Inserted Successful (top)", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); conn.close();



In Access Database 13 Columns InvoiceNumber Number, Terms Text, InvoiceDate Date/Time, OurQuote Text, SalesPerson Text, CustomerName Text, Orderno Text, InvoiceAddress Memo, DeliveryAddress Memo, InclusiveStatus Yes/No, Price Decimal, Tax Decimal, GrandTotal Decimal.

What was wrong in my code ?
Posted

the only reason this error occurs is on inserting wrong datatype to the field. Please check if the description (Datatype) about your field is mentioned correctly. And please specify what value you are supplying that gives this error.
 
Share this answer
 
Comments
agent_kruger 28-Nov-13 6:56am    
if this helped you click accept sollution.Thank you
Finally got Code thanks to all.

C#
OleDbCommand top = new OleDbCommand(
        "INSERT INTO test_top (" +
                "InvoiceNumber,Terms,[InvoiceDate],OurQuote," +
                "SalesPerson,CustomerName,OrderNumber," +
                "InvoiceAddress,DeliveryAddress,InclusiveStatus," +
                "Price,Tax,GrandTotal" +
            ") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", conn);
top.Parameters.AddWithValue("?", invoicenumber);
top.Parameters.AddWithValue("?", terms);
top.Parameters.AddWithValue("?", date);
top.Parameters.AddWithValue("?", ourquote);
top.Parameters.AddWithValue("?", salesperson);
top.Parameters.AddWithValue("?", customername);
top.Parameters.AddWithValue("?", oderno);
top.Parameters.AddWithValue("?", invoiceaddress);
top.Parameters.AddWithValue("?", deliveryaddress);
top.Parameters.AddWithValue("?", inclusive);
top.Parameters.AddWithValue("?", Price);
top.Parameters.AddWithValue("?", tax);
top.Parameters.AddWithValue("?", grandtotal);
top.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