Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
im tried to make purchase form in visual studio desktop application using C# code. i tried this code to save textbox values, datetime, and datagrideview colums value to database table and i check table and query its same but i got "number of query values and destination fields are not same" please help me to solve this error.

What I have tried:

for (int i = 0; i < dataGridView3.Rows.Count; i++)
{
cmd.CommandText = "Insert into purchase values('" + textBox1.Text + "','" + textBox17.Text + "','" + dateTimePicker1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + dataGridView3.Rows[i].Cells["Product_ID"].Value + "','" + dataGridView3.Rows[i].Cells["Product_Name"].Value +"','" + dataGridView3.Rows[i].Cells["Product_Type"].Value + "','" + dataGridView3.Rows[i].Cells["Product_Price"].Value + "','" + dataGridView3.Rows[i].Cells["Product_Qty"].Value + "','" + dataGridView3.Rows[i].Cells["Amount"].Value + "','" + textBox10.Text + "','" + textBox11.Text + "','" + textBox12.Text + "','" + textBox13.Text + "','" + textBox14.Text + "','" + textBox18.Text + "','" + textBox15.Text + "')";
        cmd.Connection = connection;
        connection.Open();
        cmd.ExecuteNonQuery();
        connection.Close();
}
C#

Posted
Updated 23-May-20 21:49pm
v2

Quote:
How to solve error number of query values and destination fields are not the same. In C# windows application

Rather simple: count number of values in query, count number of fields in table, there should be a surprise.
After that, you need to see how values and fields match.
We can't do it for you.
C#
cmd.CommandText = "Insert into purchase values('" + textBox1.Text + "','" + textBox17.Text + "','" + dateTimePicker1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + dataGridView3.Rows[i].Cells["Product_ID"].Value + "','" + dataGridView3.Rows[i].Cells["Product_Name"].Value +"','" + dataGridView3.Rows[i].Cells["Product_Type"].Value + "','" + dataGridView3.Rows[i].Cells["Product_Price"].Value + "','" + dataGridView3.Rows[i].Cells["Product_Qty"].Value + "','" + dataGridView3.Rows[i].Cells["Amount"].Value + "','" + textBox10.Text + "','" + textBox11.Text + "','" + textBox12.Text + "','" + textBox13.Text + "','" + textBox14.Text + "','" + textBox18.Text + "','" + textBox15.Text + "')";

Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
Comments
Maciej Los 22-May-20 5:31am    
5ed!
Patrice T 22-May-20 7:50am    
Thank you
The error message "number of query values and destination fields are not same" is quite easy to detect and repair...

All you have to do is to define the number of fields in which you want to insert data:
SQL
INSERT INTO purchase (field1, field2, ..., fieldN)
VALUES(value1, value2, ..., valueN)


Accordingly to instruction provided in solution #1 by Patrice T, you need to use parameters. So,
SQL
INSERT INTO purchase (field1, field2, ..., fieldN)
VALUES(@param1, @param2, ..., @paramN)


How to add parameters? Please, read this: SqlParameter Class (System.Data.SqlClient) | Microsoft Docs[^]
 
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