Deleting a Record by using RowCommand

Look at given point:

  • RowCommand is one of the event to Gridveiw.

  • This event fires whenever an action performed(Ex: button is clicked ) on Gridveiw control.

  • To perform any operations with rowcommand in Gridveiw we need to use a property known as “commandname”.

  • CommandName property is used to a button control.

    Example: commandname=”abc”.

  • In the above line abc is commandname.

  • Utilization of a commandname in itemtemplate for button is shown below.

    commandname in itemtemplate

  • Now why commandname?
CommandName is used to differentiate two button type controls within Gridveiw as shown below.

Observe the following snippet:

code

For the above code we will get the following design:

design

Code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data.SqlClient;  
  8. using System.Data;  
  9.   
  10. namespace WebApplication1  
  11. {  
  12.     public partial class WebForm2 : System.Web.UI.Page  
  13.     {  
  14.         SqlConnection con = new SqlConnection("server=.;database=abc;trusted_connection=true");  
  15.         protected void Page_Load(object sender, EventArgs e)  
  16.         {  
  17.             if (!IsPostBack)  
  18.             {  
  19.                 bind();  
  20.             }  
  21.               
  22.         }  
  23.         public void bind()  
  24.         {  
  25.             SqlCommand cmd = new SqlCommand("select * from employee", con);  
  26.             SqlDataAdapter da = new SqlDataAdapter(cmd);  
  27.             DataSet ds = new DataSet();  
  28.             da.Fill(ds, "con");  
  29.             GridView1.DataSource = ds;  
  30.             GridView1.DataBind();  
  31.         }  
  32.   
  33.         protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)  
  34.         {  
  35.               
  36.             if (e.CommandName == "comdel")  
  37.             {  
  38.   
  39.                 int l = Convert.ToInt32(e.CommandArgument);  
  40.                 con.Open();  
  41.                 SqlCommand cmd = new SqlCommand("delete from employee where eno=@a", con);  
  42.                 cmd.Parameters.AddWithValue("@a", l);  
  43.                 int i = cmd.ExecuteNonQuery();  
  44.                 if (i == 1)  
  45.                 {  
  46.                     Response.Write("Record deleted");  
  47.                     bind();  
  48.                 }  
  49.                 else  
  50.                 {  
  51.                     Response.Write("Recodrd not deleted...");  
  52.                 }  
  53.   
  54.                 Page_Load(sender, e);  
  55.             }  
  56.         }  
  57.     }  

Explanation on e.commandargument:

In the above code I used the following line in rowcommand event.
  1. if (e.CommandName == "comdel")  
  2. {  
  3.   
  4.    int l = Convert.ToInt32(e.CommandArgument);  
  5.    …..  
  6.    …  
  7.    …  

e.commandargument specifies the employee number. Observe the following aspx code snippet.

commandargument
If still any doubts plzz feel free to ask.

Thank you.