Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, am trying to store my textedit valued to MS Access Database but am getting error like this "Number of query values and destination fields are not the same". But my query values and destination fields are same. I try to inserting in two methods both method getting same error.

I tried this method first
C#
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;

       decimal wholediscper = Convert.ToDecimal(TXE_FlatDiscountP.Text);
       decimal wholediscamt = Convert.ToDecimal(TXE_FlatDiscountA.Text);
       decimal shippingpercenatge = Convert.ToDecimal(TXE_ShippingPercentage.Text);
       decimal shippingamount = Convert.ToDecimal(TXE_ShippingAmount.Text);
       decimal unitprice = Convert.ToDecimal(TXE_SubTotal.Text);
       decimal discount = Convert.ToDecimal(TXE_Discount.Text);
       decimal tax = Convert.ToDecimal(TXE_Tax.Text);
       decimal shiping = Convert.ToDecimal(TXE_Shipping.Text);
       decimal grandtotal = Convert.ToDecimal(TXE_GrandTotal.Text);


       OleDbCommand top = new OleDbCommand(
           "INSERT INTO NewInvoice_1 (" +
           "InvoiceNumber,Terms,[InvoiceDate],OurQuote," +
           "SalesPerson,CustomerName,OrderNumber," +
           "InvoiceAddress,DeliveryAddress," +
           "WholeDiscountP,WholeDiscountA,ShippingP,ShippingA" +
           "Price,Discount,Tax" +
           "Shipping,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("?", wholediscper);
       top.Parameters.AddWithValue("?", wholediscamt);
       top.Parameters.AddWithValue("?", shippingpercenatge);
       top.Parameters.AddWithValue("?", shippingamount);
       top.Parameters.AddWithValue("?", unitprice);
       top.Parameters.AddWithValue("?", discount);
       top.Parameters.AddWithValue("?", tax);
       top.Parameters.AddWithValue("?", shiping);
       top.Parameters.AddWithValue("?", grandtotal);

       top.ExecuteNonQuery();


Second method

C#
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;

        decimal wholediscper = Convert.ToDecimal(TXE_FlatDiscountP.Text);
        decimal wholediscamt = Convert.ToDecimal(TXE_FlatDiscountA.Text);
        decimal shippingpercenatge = Convert.ToDecimal(TXE_ShippingPercentage.Text);
        decimal shippingamount = Convert.ToDecimal(TXE_ShippingAmount.Text);
        decimal unitprice = Convert.ToDecimal(TXE_SubTotal.Text);
        decimal discount = Convert.ToDecimal(TXE_Discount.Text);
        decimal tax = Convert.ToDecimal(TXE_Tax.Text);
        decimal shiping = Convert.ToDecimal(TXE_Shipping.Text);
        decimal grandtotal = Convert.ToDecimal(TXE_GrandTotal.Text);

        OleDbCommand top = new OleDbCommand("INSERT INTO test_top(InvoiceNumber,Terms,[InvoiceDate],OurQuote,SalesPerson,CustomerName,OrderNumber,InvoiceAddress,DeliveryAddress,WholeDiscountP,WholeDiscountA,ShippingP,ShippingA,Price,Discount,Tax,Shipping,GrandTotal) VALUES (" + invoicenumber + ",'" + terms + "','" + date + "','" + ourquote + "','" + salesperson + "','" + customername + "','" + oderno + "','" + invoiceaddress + "','" + deliveryaddress + "',"+ wholediscper +","+ wholediscamt +","+ shippingpercenatge +","+ shippingamount +"," + unitprice + "," + tax + "," + grandtotal + ")", conn);


In Both method getting same error ? Whats wrong with my code ? Help me
Posted

The discount and shiping values are missing in second method.
Try again by adding them.
 
Share this answer
 
First is wrong as you need to name the parameters in query and when adding the values.
Second is correct except that you are defined 2 additional column for insert statement and not passing it's value.
First missing value is for Discount & Second is of Shipping.

So, correct code would be some thing like

C#
OleDbCommand top = new OleDbCommand("INSERT INTO test_top(InvoiceNumber,Terms,[InvoiceDate],OurQuote,SalesPerson,CustomerName,OrderNumber,InvoiceAddress,DeliveryAddress,WholeDiscountP,WholeDiscountA,ShippingP,ShippingA,Price,Discount,Tax,Shipping,GrandTotal) VALUES (" + invoicenumber + ",'" + terms + "','" + date + "','" + ourquote + "','" + salesperson + "','" + customername + "','" + oderno + "','" + invoiceaddress + "','" + deliveryaddress + "',"+ wholediscper +","+ wholediscamt +","+ shippingpercenatge +","+ shippingamount +"," + unitprice + "," + discount + "," +tax + "," + shipping + "," + grandtotal + ")", conn);
 
Share this answer
 
its because here
OleDbCommand top = new OleDbCommand(
            "INSERT INTO NewInvoice_1 (" +
            "InvoiceNumber,Terms,[InvoiceDate],OurQuote," +
            "SalesPerson,CustomerName,OrderNumber," +
            "InvoiceAddress,DeliveryAddress," +
            "WholeDiscountP,WholeDiscountA,ShippingP,ShippingA" +
            "Price,Discount,Tax" +
            "Shipping,GrandTotal" +
            ") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", conn);


you have written 17 values and passing and passing 18 values. According to me "ShippingA,Price" should be written instead of "ShippingA" + "Price".

If your query is answered please click accept solution.
 
Share this answer
 
v2

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