Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void gid_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Label lblname = (Label)gid.Rows[e.RowIndex].FindControl("lblname");
            con.Open();
          string cmdd = "DELETE from grid where name='" + lblname + "'";
          
          cmd = new SqlCommand(cmdd,con);
          cmd.Parameters.AddWithValue("@name", lblname.Text);
           
            cmd.ExecuteNonQuery();
            con.Close();
            fill_show();
        }

code not work properly
please give solution

What I have tried:

C#
protected void gid_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Label lblname = (Label)gid.Rows[e.RowIndex].FindControl("lblname");
            con.Open();
          string cmdd = "DELETE from grid where name='" + lblname + "'";
          
          cmd = new SqlCommand(cmdd,con);
          cmd.Parameters.AddWithValue("@name", lblname.Text);
           
            cmd.ExecuteNonQuery();
            con.Close();
            fill_show();
        }
Posted
Updated 18-Aug-17 0:47am
v2
Comments
Prifti Constantine 18-Aug-17 2:52am    
Why are you writing the sql Command in this situation?
Libin C Jacob 18-Aug-17 3:15am    
Is it deleting from the database side?
Sinisa Hajnal 18-Aug-17 3:49am    
You don't have defined parameter @name in your query. Correct it and it should work. It will also save you from SQL injection - you should NEVER add text from the user directly into the query.

1 solution

correction

C#
string cmdd = "DELETE from grid where name=@name ";
            cmd = new SqlCommand(cmdd, con);
            cmd.Parameters.AddWithValue("@name", lblname.Text);
            cmd.ExecuteNonQuery();
 
Share this answer
 
v2

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