Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
XML
while i was searching in google i find a code to add the items from datagridview to database using c# 

<pre lang="c#">for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {



                string strCmd = @"Insert into Bills(itemname) values(" + dataGridView1.Rows[i].Cells[0].Value + ")";
                SqlCommand Cmd = new SqlCommand(strCmd, Con);
                Cmd.ExecuteNonQuery();

            }


the was like that dataGridView1.Rows[i].Cells[1].Value + ")"; when i chang it to .Cells[0]

i got that error

incorrect syntax near 3
Posted
Comments
Richard Deeming 4-Aug-15 10:02am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
ÃHmed Élkady 4-Aug-15 10:03am    
i use parameters but now iam trying after i make the code i convert it to parameters
[no name] 4-Aug-15 10:05am    
Why not just do it correctly to begin with?
Richard Deeming 4-Aug-15 10:06am    
Bad idea. There's a good chance that you'll either forget or run out of time. You're also making problems for yourself which wouldn't exist if you started with properly parameterized queries.
ÃHmed Élkady 4-Aug-15 10:10am    
how i can fix that

1 solution

Using a properly parameterized query will fix the SQL Injection[^] vulnerability in your code, and also fix the error you're seeing:
C#
using (SqlConnection connection = new SqlConnection("-YOUR CONNECTION STRING-"))
using (SqlCommand command = new SqlCommand("INSERT INTO Bills (ItemName) VALUES (@ItemName)", connection))
{
    SqlParameter pItemName = command.Parameters.Add("@ItemName", SqlDbType.VarChar, 50);

    connection.Open();
    
    for (int i = 0; i < dataGridView1.Rows.Count; i++)
    {
        pItemName.Value = dataGridView1.Rows[i].Cells[0].Value ?? DBNull.Value;
        command.ExecuteNonQuery();
    }
}


Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
SQL injection attack mechanics | Pluralsight [^]
 
Share this answer
 
v3
Comments
ÃHmed Élkady 4-Aug-15 10:16am    
ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.

???
Richard Deeming 4-Aug-15 10:21am    
Sorry, I missed the connection.Open() call. Try the updated solution.
ÃHmed Élkady 4-Aug-15 10:24am    
The parameterized query '(@ItemName varchar(50))INSERT INTO Bills (itemname) VALUES (@ite' expects the parameter '@ItemName', which was not supplied.
ÃHmed Élkady 4-Aug-15 10:27am    
i will se :) (Y)
ÃHmed Élkady 4-Aug-15 10:28am    
The parameterized query '(@ItemName varchar(50))INSERT INTO Bills (itemname) VALUES (@Ite' expects the parameter '@ItemName', which was not supplied.

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