Deletion of records in asp.net using C#

In this tutorial we will learn how to perform the deletion of records in asp.net using C#. First of all we will show all records coming from database in gridview and then using gridview’s built-in event RowDeleting we will perform deletion of records in asp.bet using C#. To perform deletion of records in aso.net using C# is quite easy.

deleting_records_in_gridview.aspx

<asp:gridview id="GridView1" runat="server" width="100%">

<Columns>

<asp:TemplateField HeaderText="ID" >
<ItemTemplate>
<asp:Label ID="lbl_UserID" Text='<%# Bind("User_ID")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" >
<ItemTemplate>
<asp:Label ID="lbl_Email" Text='<%# Bind("User_Email")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" >
<ItemTemplate>
<asp:Label ID="lbl_first_name" Text='<%# Bind("User_First_Name")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" >
<ItemTemplate>
<asp:Label ID="lbl_last_name" CssClass="aLinkButton" Text='<%# Bind("User_Last_Name")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Delete" >                       

<ItemTemplate>

<asp:Label ID="lbl_delete" Visible="false"  Text='<%# Bind("User_ID")%>' runat="server"></asp:Label>                        

<asp:ImageButton runat="server" ID="img" ImageUrl="images/b_drop.png" CommandName="Delete"  />

</ItemTemplate>                        

</asp:TemplateField>
</Columns>
</asp:gridview>

 

deleting_records_in_gridview.aspx.cs

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;


public partial class Tutorials_deleting_data_in_gridview : System.Web.UI.Page
{
string strcon = "server=localhost;database=tutorials-db; uid=adeel;password=pakistan; Connect Timeout=200; Max Pool Size=5000"; 


SqlConnection con = new SqlConnection(); 

SqlCommand com = new SqlCommand(); 

DataSet ds = new DataSet(); 

SqlDataAdapter da; 

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
} 


private void bindgrid()
{
ds.Clear();
con.ConnectionString = strcon; 


com.Connection = con;

con.Open();

string strQuery = "select * from users"; 

da = new SqlDataAdapter(strQuery,con);

da.Fill(ds); 


GridView1.DataSource = ds;

GridView1.DataBind();

con.Close();
}

//Deleting records from database in asp.net using C#

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

    {

  con.ConnectionString=strcon;

        com.Connection=con;

        con.Open();

        int index = e.RowIndex;        

        GridViewRow row=GridView1.Rows[index];

        Label lbl = (Label)row.FindControl("lbl_delete");

        string id = lbl.Text;

        string strDelete = "delete from users where User_ID='"+id+"'";

        com.CommandText = strDelete;

        com.ExecuteNonQuery();

con.Close();

        bindgrid();

  }

//Deleting records function ends here

}

So this is the way to perform deletion of data/records in asp.net using C#

Happy Coding, Keep Codig…!

 

 

0 comments: