SQL Injection in asp.net




protected voidButton1_Click(object sender, EventArgs e)
 {
  using (SqlConnectioncon = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString))
 {
 SqlCommand cmd = newSqlCommand();
 cmd.CommandText = "Select *from ProductTable where ProductName like '" + TextBox1.Text + "%'";
            Response.Write(cmd.CommandText);
            cmd.Connection = con;
            con.Open();
            GridView1.DataSource = cmd.ExecuteReader();    // Second method gvdatasource
            GridView1.DataBind();
 }

---------------------------------same work Prevention--------------------
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString))
        {
            SqlCommand cmd = newSqlCommand();
            cmd.CommandText = "Select *from ProductTable where ProductName like @ProductName";
            cmd.Parameters.AddWithValue("@ProductName", TextBox1.Text + "%");
            Response.Write(cmd.CommandText);
            cmd.Connection = con;
            con.Open();
            GridView1.DataSource = cmd.ExecuteReader();    // Second method Gvdatasource
            GridView1.DataBind();
        }


Comments

Popular posts from this blog

Creating a DDL File & link ddl file in C#

SQL Command in asp.net