Click here to Skip to main content
15,664,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Please am working on a project in c# and I want to insert all data on datagridview into SQL table. I tried this code and I get this exception: "Additional information: Incorrect syntax near ','."
This is my code block

C#
private void button23_Click(object sender, EventArgs e)
       {
      
 SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                con.Open();

                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    strquery = "insert into Febuary15 values (" + dataGridView1.Rows[i].Cells[0].Value + "," + dataGridView1.Rows[i].Cells[1].Value + "," + dataGridView1.Rows[i].Cells[2].Value + ", "+
                   " " + dataGridView1.Rows[i].Cells[3].Value + "," + dataGridView1.Rows[i].Cells[4].Value + "," + dataGridView1.Rows[i].Cells[5].Value + ", " +
                   " " + dataGridView1.Rows[i].Cells[6].Value + "," + dataGridView1.Rows[i].Cells[7].Value + "," + dataGridView1.Rows[i].Cells[8].Value + ", " +
                    " " + dataGridView1.Rows[i].Cells[9].Value + "," + dataGridView1.Rows[i].Cells[10].Value + "," + dataGridView1.Rows[i].Cells[11].Value + ", " +
                   " " + dataGridView1.Rows[i].Cells[12].Value + ", " + dataGridView1.Rows[i].Cells[13].Value + ", " + dataGridView1.Rows[i].Cells[14].Value + ", " +
                   " " + dataGridView1.Rows[i].Cells[15].Value + "," + dataGridView1.Rows[i].Cells[16].Value + "," + dataGridView1.Rows[i].Cells[17].Value + ", " +
                   " " + dataGridView1.Rows[i].Cells[18].Value + "," + dataGridView1.Rows[i].Cells[19].Value + "," + dataGridView1.Rows[i].Cells[20].Value + ", " +
                   " " + dataGridView1.Rows[i].Cells[21].Value + "," + dataGridView1.Rows[i].Cells[22].Value + "," + dataGridView1.Rows[i].Cells[23].Value + ", " +
                   " " + dataGridView1.Rows[i].Cells[24].Value + "," + dataGridView1.Rows[i].Cells[25].Value + "," + dataGridView1.Rows[i].Cells[26].Value + ", " +
                   " " + dataGridView1.Rows[i].Cells[27].Value + "," + dataGridView1.Rows[i].Cells[28].Value + "," + dataGridView1.Rows[i].Cells[29].Value + ", " +
                   " " + dataGridView1.Rows[i].Cells[30].Value + "," + dataGridView1.Rows[i].Cells[31].Value + "," + dataGridView1.Rows[i].Cells[32].Value + ", " +
                   " " + dataGridView1.Rows[i].Cells[33].Value + "," + dataGridView1.Rows[i].Cells[34].Value + "," + dataGridView1.Rows[i].Cells[35].Value + ", " +
                   " " + dataGridView1.Rows[i].Cells[36].Value + ")";
                    cmd.CommandText = strquery;
                    cmd.ExecuteNonQuery();
                    con.Close();

                }
                MessageBox.Show("Data Inserted Successfully");
            }


Please help. Thanks

What I have tried:

I have tried other codes but still not working
Posted
Updated 31-Mar-21 20:46pm
v2

1 solution

I see no obvious error in your query, but since the query syntax depends on contain of your datagridview, it is impossible to tell for sure.
C#
 strquery = "insert into Febuary15 values (" + dataGridView1.Rows[i].Cells[0].Value + "," + dataGridView1.Rows[i].Cells[1].Value + "," + dataGridView1.Rows[i].Cells[2].Value + ", "+
" " + dataGridView1.Rows[i].Cells[3].Value + "," + dataGridView1.Rows[i].Cells[4].Value + "," + dataGridView1.Rows[i].Cells[5].Value + ", " +
" " + dataGridView1.Rows[i].Cells[6].Value + "," + dataGridView1.Rows[i].Cells[7].Value + "," + dataGridView1.Rows[i].Cells[8].Value + ", " +
 " " + dataGridView1.Rows[i].Cells[9].Value + "," + dataGridView1.Rows[i].Cells[10].Value + "," + dataGridView1.Rows[i].Cells[11].Value + ", " +
" " + dataGridView1.Rows[i].Cells[12].Value + ", " + dataGridView1.Rows[i].Cells[13].Value + ", " + dataGridView1.Rows[i].Cells[14].Value + ", " +
" " + dataGridView1.Rows[i].Cells[15].Value + "," + dataGridView1.Rows[i].Cells[16].Value + "," + dataGridView1.Rows[i].Cells[17].Value + ", " +
" " + dataGridView1.Rows[i].Cells[18].Value + "," + dataGridView1.Rows[i].Cells[19].Value + "," + dataGridView1.Rows[i].Cells[20].Value + ", " +
" " + dataGridView1.Rows[i].Cells[21].Value + "," + dataGridView1.Rows[i].Cells[22].Value + "," + dataGridView1.Rows[i].Cells[23].Value + ", " +
" " + dataGridView1.Rows[i].Cells[24].Value + "," + dataGridView1.Rows[i].Cells[25].Value + "," + dataGridView1.Rows[i].Cells[26].Value + ", " +
" " + dataGridView1.Rows[i].Cells[27].Value + "," + dataGridView1.Rows[i].Cells[28].Value + "," + dataGridView1.Rows[i].Cells[29].Value + ", " +
" " + dataGridView1.Rows[i].Cells[30].Value + "," + dataGridView1.Rows[i].Cells[31].Value + "," + dataGridView1.Rows[i].Cells[32].Value + ", " +
" " + dataGridView1.Rows[i].Cells[33].Value + "," + dataGridView1.Rows[i].Cells[34].Value + "," + dataGridView1.Rows[i].Cells[35].Value + ", " +
" " + dataGridView1.Rows[i].Cells[36].Value + ")";

Not 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
emma aboagye 31-Mar-21 19:08pm    
OK thanks
Maciej Los 1-Apr-21 2:47am    
5ed!
Patrice T 1-Apr-21 2:49am    
Thank you

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