Friday, June 24, 2011

Delete records in database using 3-tier architecture in asp.net with c#

In this programming tutorial you will learn how to delete the records in database using 3-tier architecture in asp.net with c#. For beginners ,I strongly recommend to read this tutorial 3-Tier Architecture in asp.net using c# first to get basics of 3-tier architecture in asp.net. So let’s begin, have a look over .aspx page
delete records in database using 3-tier architecture in asp.net with c#
Delete records in database using 3-tier architecture in asp.net with c#

delete-records.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="delete-records.aspx.cs" Inherits="delete_records" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5. <title> Delete records in database using 3-tier architecture in asp.net with c#</title>  
  6. </head>  
  7. <body>  
  8. <form id="form1" runat="server">  
  9. <asp:GridView ID="GridView1" runat="server" EnableTheming="false" AutoGenerateColumns="false"  
  10.         GridLines="None" Width="100%" OnRowDeleting="GridView1_RowDeleting"  
  11. >  
  12.         <Columns>  
  13.             <asp:TemplateField HeaderText="Roll No" HeaderStyle-Width="10%" HeaderStyle-HorizontalAlign="Center">  
  14.                 <ItemTemplate>  
  15.                     <asp:Label ID="lblRollNo" runat="server" EnableTheming="false" Text='<%# Bind("rollNo")%>'></asp:Label>  
  16.                 </ItemTemplate>  
  17.                 <ItemStyle HorizontalAlign="Center" />  
  18.             </asp:TemplateField>  
  19.             <asp:TemplateField HeaderText="First Name" HeaderStyle-Width="15%" HeaderStyle-HorizontalAlign="Left">  
  20.                 <ItemTemplate>  
  21.                     <asp:Label ID="lblFirstName" runat="server" EnableTheming="false" Text='<%# Bind("firstName")%>'></asp:Label>  
  22.                 </ItemTemplate>  
  23.                 <ItemStyle HorizontalAlign="Left" />  
  24.             </asp:TemplateField>  
  25.             <asp:TemplateField HeaderText="Last Name" HeaderStyle-Width="15%" HeaderStyle-HorizontalAlign="Left">  
  26.                 <ItemTemplate>  
  27.                     <asp:Label ID="lblLastName" runat="server" EnableTheming="false" Text='<%# Bind("lastName")%>'></asp:Label>  
  28.                 </ItemTemplate>  
  29.                 <ItemStyle HorizontalAlign="Left" />  
  30.             </asp:TemplateField>  
  31.             <asp:TemplateField HeaderText="Gender" HeaderStyle-Width="15%" HeaderStyle-HorizontalAlign="Left">  
  32.                 <ItemTemplate>  
  33.                     <asp:Label ID="lblGender" runat="server" EnableTheming="false" Text='<%# Bind("gender")%>'></asp:Label>  
  34.                 </ItemTemplate>  
  35.                 <ItemStyle HorizontalAlign="Left" />  
  36.             </asp:TemplateField>  
  37.             <asp:TemplateField HeaderText="Delete" HeaderStyle-Width="10%" HeaderStyle-HorizontalAlign="Center">  
  38.                 <ItemTemplate>  
  39.                     <asp:Label ID="lblDelete" Visible="false" Text='<%# Bind("rollNo")%>' runat="server"></asp:Label>  
  40.                     <asp:ImageButton runat="server" ID="img" ImageUrl="images/delete.gif" CommandName="Delete"  
  41.                         OnClientClick="return window.confirm('Are you sure you want to delete this record?')" />  
  42.                 </ItemTemplate>  
  43.                 <ItemStyle HorizontalAlign="Center" />  
  44.             </asp:TemplateField>  
  45.         </Columns>  
  46.     </asp:GridView>  
  47. </form>  
  48. </body>  
  49. </html>  
As you have seen, we have an asp:gridview control in our web page. We have four template fields in it, these are rollNo, firstName, lastName and Delete image to delete the records. The main thing you must have to notice is the OnRowDeleting event of gridview. Through this event we will delete the records from database. So let’s have a look over its c# code behind file.

delete-records.aspx.cs
  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.HtmlControls;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.WebControls.WebParts;  
  11. using BusinessLayer;//Don’t forget to include this namespace  
  12.   
  13. public partial class delete_records : System.Web.UI.Page  
  14. {  
  15.     BusStudent _objStudent = new BusStudent();  
  16.   
  17.     protected void Page_Load(object sender, EventArgs e)  
  18.     {  
  19.         if (!Page.IsPostBack)  
  20.         {  
  21.                   
  22.                 BindGrid();  
  23.               
  24.         }  
  25.   
  26.     }  
  27.   
  28.     public void BindGrid()  
  29.     {  
  30.         _objStudent.GetStudentRecords();  
  31.         GridView1.DataSource = _objStudent.StudentDS.Tables["studentrecords"];  
  32.         GridView1.DataBind();  
  33.     }  
  34.   
  35.     protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)  
  36.     {  
  37.         int index = e.RowIndex;  
  38.         GridViewRow row = GridView1.Rows[index];  
  39.         //The asp:label just before delete image  
  40.         Label lbl = (Label)row.FindControl("lblDelete");  
  41.         int rollNo = Convert.ToInt32(lbl.Text);  
  42.         //Deleting Records From Database      
  43.         _objStudent.DeleteStudentRows(rollNo);  
  44.        //Again Populating Gridview after records deletion  
  45.         BindGrid();  
  46.     }  
  47. }  
BusStudent.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Data;  
  5. using DataAccessLayer;//Donot forget to import this namespace  
  6.   
  7. namespace BusinessLayer  
  8. {  
  9. public class BusStudent  
  10. {  
  11. DbAccess _dbAccess = new DbAccess();  
  12. private DataSet _StudentDS = new DataSet();  
  13. public DataSet StudentDS  
  14. {  
  15. get  
  16. {  
  17. return _StudentDS;  
  18. }  
  19. set  
  20. {  
  21. _StudentDS = value;  
  22. }  
  23. }  
  24.   
  25.         public void GetStudentRecords()  
  26.         {  
  27.   
  28.             try  
  29.             {  
  30.                 string strQuery = "select rollNo,firstName,lastName,gender from web_tbl_student";  
  31.                 if (_StudentDS.Tables.Contains("studentrecords"))  
  32.                 {  
  33.                     _StudentDS.Tables["studentrecords"].Clear();  
  34.                 }  
  35.                 _dbAccess.selectQuery(_StudentDS, strQuery, "studentrecords");  
  36.             }  
  37.             catch (Exception ex)  
  38.             {  
  39.                 throw ex;  
  40.             }  
  41.   
  42.         }  
  43.   
  44.   //Function to delete records from database, in this example I used MS SQL SERVER as a backend database     
  45.   
  46.         public void DeleteStudentRows(int rollNo)  
  47.         {  
  48.             try  
  49.             {  
  50.        // I will always recommend you to not delete the records physically  
  51.                 string strQuery = "delete from web_tbl_student where rollNo='"+ rollNo +"'";  
  52.                 _dbAccess.executeQuery(strQuery);  
  53.             }  
  54.             catch (Exception ex)  
  55.             {  
  56.                 throw ex;  
  57.             }  
  58.   
  59.         }  
  60.   
  61.   
  62.   
  63. }  
  64.   
  65. }  

Every thing is self explanatory and I will not go into any details as I already have written two tutorials 3-Tier Architecture in asp.net using c# and Insertion of records in database using 3-tier architecture in asp.net with c#.

If anything is not cleared then please read these above mentioned tutorials first. So this is the way to delete records in database using 3-tier architecture in asp.net with c#.

I love your feedback.

No comments:

Post a Comment