Click here to Skip to main content
15,917,473 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
protected void Submit(object sender, EventArgs e)
{
    string connectionString = ConfigurationManager.ConnectionStrings["CustomerDB"].ConnectionString;
    using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers WHERE CustomerId = '" + txtCustomerId.Text + "'"))
    {
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            con.Open();
            cmd.Connection = con;
            CustomerGridView.DataSource = cmd.ExecuteNonQuery();
            CustomerGridView.DataBind();
            con.Close();
        }
    }
}


What I have tried:

1.What is wrong with the following code?
Posted
Updated 11-Jun-16 21:14pm
v2
Comments
CHill60 11-Jun-16 11:49am    
It is vulnerable to SQL Injection attacks. Why are you asking?
hasithakaru 11-Jun-16 11:49am    
What is wrong with the following code? What alternatives would you recommend?
hasithakaru 11-Jun-16 11:51am    
This is assignment question.So I need to answer for this
CHill60 11-Jun-16 11:54am    
Don't keep posting the same code, you've already shared it in the original question.
Answer: It is vulnerable to SQL Injection attacks
hasithakaru 11-Jun-16 11:56am    
Ok thanks.WHat alterntive u recommand?

As stated above:

The code uses a concatenated string to create a sql query. This leaves it vulnerable to SQL Injection attacks.

Alternative: Use parameterised queries.

Articles on the subject:
Give me parameterized SQL, or give me death[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
Comments
BillWoodruff 11-Jun-16 23:42pm    
Amen.
Two things you have to consider
  • SqlInjection attack which you can refer from above answers
  • you are using SqlCommand.ExecuteNonQuery [^] which will result only integer value indicating the no of rows affected where cannot show the details of customer in the gridview, you should use sqldataadapter[^] or SqlDataReader[^] to fetch the data and show it in the girdview



C#
string connectionString = ConfigurationManager.ConnectionStrings["CustomerDB"].ConnectionString;
           using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers WHERE CustomerId = @customer"))
           {
               using (SqlConnection con = new SqlConnection(connectionString))
               {
                   cmd.Connection = con;
                   cmd.Parameters.Add("@customer", txtCustomerId.Text.Trim());
                   SqlDataAdapter da = new SqlDataAdapter(cmd);
                   DataTable dt = new DataTable();
                   da.Fill(dt);
                   CustomerGridView.DataSource = dt;
                   CustomerGridView.DataBind();

               }
           }


Since sqldataadapter[^] is a disconnected Architecture, No need to Open and close the connection manually. it will be taken care off. and since you are using SqlConnection under Using block, the close() part will be handled internally when the object gets disposed.
 
Share this answer
 
Your approach is wrong from the very beginning. The query composed by concatenation with strings taken from UI. Not only repeated string concatenation is inefficient (because strings are immutable; do I have to explain why it makes repeated concatenation bad?), but there is way more important issue: it opens the doors to a well-known exploit called SQL injection.

This is how it works: http://xkcd.com/327.

Are you getting the idea? The string taken from a control can be anything, including… a fragment of SQL code.

What to do? Just read about this problem and the main remedy: parametrized statements: http://en.wikipedia.org/wiki/SQL_injection.

With ADO.NET, use this: http://msdn.microsoft.com/en-us/library/ff648339.aspx.

Please see my past answers for some more detail:
EROR IN UPATE in com.ExecuteNonQuery();,
hi name is not displaying in name?.

See also (links provided by our member Richard Deeming:
Troy Hunt: Everything you wanted to know about SQL injection (but were afraid to ask),
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange,
Query Parameterization Cheat Sheet — OWASP[^],
SQL injection attack mechanics | Pluralsight — YouTube.

—SA
 
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