CRUD Operations Using Three Tier in ASP.NET

In three tier Architecture we have Business Layer,DataAccess Layer,Presentation Layer and a Schema which has properties to set values.
 
First open sql server and Create a table name Employee_Test Include following columns,
 
Id int not null primary key,
Name varchar(50),
Address varchar(50),
Age int
 
Then Create a Stored Procedure name Insert_User_Data, Following is the Stored Procedure:
  1. Create PROCEDURE Insert_User_Data (  
  2. @Para varchar(50)= '',  
  3. @Id int = 0,  
  4. @Name varchar(50)= '',  
  5. @Address varchar(50)= '',  
  6. @Age int = 0  
  7. AS BEGIN If @Para = 'ADD' Begin Insert into Employee_Test(Name, Address, Age)  
  8. values  
  9. (@Name, @Address, @Age);  
  10. END Else If @Para = 'Get_For_Grid' Begin  
  11. Select  
  12. *  
  13. from  
  14. Employee_Test END Else If @Para = 'Get_By_Id' Begin  
  15. Select  
  16. Name,  
  17. Address,  
  18. Age  
  19. from  
  20. Employee_Test  
  21. where  
  22. Id = @Id;  
  23. END Else If @Para = 'Update' Begin  
  24. Update  
  25. Employee_Test  
  26. Set  
  27. Name = @Name,  
  28. Address = @Address,  
  29. Age = @Age  
  30. where  
  31. Id = @Id;  
  32. END Else If @Para = 'Delete' Begin  
  33. Delete from  
  34. Employee_Test  
  35. where  
  36. Id = @Id;  
  37. END END  
Go to Microsoft visual studio,then file-new project and select asp.net WebApplication Right click on solution in Solution Explorer and click add new project and in the windows tab Select class library and name it as EmployeeSchema and insert following code in it,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace EmployeeSchema {  
  6.     public class Class1 {  
  7.         private string _Name;  
  8.         private string _Address;  
  9.         private int _Age;  
  10.         public string Name {  
  11.             get {  
  12.                 return _Name;  
  13.             }  
  14.             set {  
  15.                 _Name = value;  
  16.             }  
  17.         }  
  18.         public string Address {  
  19.             get {  
  20.                 return _Address;  
  21.             }  
  22.             set {  
  23.                 _Address = value;  
  24.             }  
  25.         }  
  26.         public int Age {  
  27.             get {  
  28.                 return _Age;  
  29.             }  
  30.             set {  
  31.                 _Age = value;  
  32.             }  
  33.         }  
  34.     }  
  35. }  
In the same way create one more class library and name it to EmployeeDAL and write following code to it.Add Reference of EmployeeSchema to EmployeeDAL. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data.SqlClient;  
  6. using System.Data;  
  7. using System.Configuration;  
  8. using EmployeeSchema;  
  9. namespace EmployeeDAL {  
  10.     public class Class1 {  
  11.         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());  
  12.         SqlCommand cmd;  
  13.         DataTable dt;  
  14.         public int InsertData(EmployeeSchema.Class1 objSchema) {  
  15.             try {  
  16.                 using(cmd = new SqlCommand("Insert_User_Data", con)) {  
  17.                     cmd.CommandType = CommandType.StoredProcedure;  
  18.                     cmd.Parameters.AddWithValue("@Para""ADD");  
  19.                     cmd.Parameters.AddWithValue("@Name", objSchema.Name);  
  20.                     cmd.Parameters.AddWithValue("@Address", objSchema.Address);  
  21.                     cmd.Parameters.AddWithValue("@Age", Convert.ToInt32(objSchema.Age));  
  22.                     if (con.State.Equals(ConnectionState.Closed)) con.Open();  
  23.                     int result = cmd.ExecuteNonQuery();  
  24.                     con.Close();  
  25.                     return result;  
  26.                 }  
  27.             } catch (Exception ex) {  
  28.                 throw ex;  
  29.             } finally {  
  30.                 con.Close();  
  31.             }  
  32.         }  
  33.         public int UpdateData(EmployeeSchema.Class1 objSchema, int Id) {  
  34.             try {  
  35.                 using(cmd = new SqlCommand("Insert_User_Data", con)) {  
  36.                     cmd.CommandType = CommandType.StoredProcedure;  
  37.                     cmd.Parameters.AddWithValue("@Para""Update");  
  38.                     cmd.Parameters.AddWithValue("@Id", Id);  
  39.                     cmd.Parameters.AddWithValue("@Name", objSchema.Name);  
  40.                     cmd.Parameters.AddWithValue("@Address", objSchema.Address);  
  41.                     cmd.Parameters.AddWithValue("@Age", Convert.ToInt32(objSchema.Age));  
  42.                     if (con.State.Equals(ConnectionState.Closed)) con.Open();  
  43.                     int result = cmd.ExecuteNonQuery();  
  44.                     con.Close();  
  45.                     return result;  
  46.                 }  
  47.             } catch (Exception ex) {  
  48.                 throw ex;  
  49.             } finally {  
  50.                 con.Close();  
  51.             }  
  52.         }  
  53.         public int DeleteData(int Id) {  
  54.             try {  
  55.                 using(cmd = new SqlCommand("Insert_User_Data", con)) {  
  56.                     cmd.CommandType = CommandType.StoredProcedure;  
  57.                     cmd.Parameters.AddWithValue("@Para""Delete");  
  58.                     cmd.Parameters.AddWithValue("@Id", Id);  
  59.                     if (con.State.Equals(ConnectionState.Closed)) con.Open();  
  60.                     int result = cmd.ExecuteNonQuery();  
  61.                     con.Close();  
  62.                     return result;  
  63.                 }  
  64.             } catch (Exception ex) {  
  65.                 throw ex;  
  66.             } finally {  
  67.                 con.Close();  
  68.             }  
  69.         }  
  70.         public DataTable BindGrid() {  
  71.             using(cmd = new SqlCommand("Insert_User_Data", con)) {  
  72.                 try {  
  73.                     cmd.CommandType = CommandType.StoredProcedure;  
  74.                     cmd.Parameters.AddWithValue("@Para""Get_For_Grid");  
  75.                     if (con.State.Equals(ConnectionState.Closed)) con.Open();  
  76.                     SqlDataAdapter da = new SqlDataAdapter(cmd);  
  77.                     dt = new DataTable();  
  78.                     da.Fill(dt);  
  79.                     con.Close();  
  80.                     return dt;  
  81.                 } catch (Exception ex) {  
  82.                     throw ex;  
  83.                 }  
  84.             }  
  85.         }  
  86.         public DataTable GetById(int Id) {  
  87.             using(cmd = new SqlCommand("Insert_User_Data", con)) {  
  88.                 try {  
  89.                     cmd.CommandType = CommandType.StoredProcedure;  
  90.                     cmd.Parameters.AddWithValue("@Para""Get_By_Id");  
  91.                     cmd.Parameters.AddWithValue("@Id", Id);  
  92.                     if (con.State.Equals(ConnectionState.Closed)) con.Open();  
  93.                     SqlDataAdapter da = new SqlDataAdapter(cmd);  
  94.                     dt = new DataTable();  
  95.                     da.Fill(dt);  
  96.                     con.Close();  
  97.                     return dt;  
  98.                 } catch (Exception ex) {  
  99.                     throw ex;  
  100.                 }  
  101.             }  
  102.         }  
  103.     }  
  104. }  
In the same way add another class library and name it to EmployeeBAL and add reference of EmployeeDAL to EmployeeBAL
and write following code in it,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using EmployeeSchema;  
  6. using EmployeeDAL;  
  7. using System.Data;  
  8. namespace EmployeeBAL {  
  9.     public class Class1 {  
  10.         public int Insert(EmployeeSchema.Class1 objSchema) {  
  11.             try {  
  12.                 EmployeeDAL.Class1 objDAL = new EmployeeDAL.Class1();  
  13.                 return objDAL.InsertData(objSchema);  
  14.             } catch (Exception ex) {  
  15.                 throw ex;  
  16.             }  
  17.         }  
  18.         public int Update(EmployeeSchema.Class1 objSchema, int Id) {  
  19.             try {  
  20.                 EmployeeDAL.Class1 objDAL = new EmployeeDAL.Class1();  
  21.                 return objDAL.UpdateData(objSchema, Id);  
  22.             } catch (Exception ex) {  
  23.                 throw ex;  
  24.             }  
  25.         }  
  26.         public int Delete(int Id) {  
  27.             try {  
  28.                 EmployeeDAL.Class1 objDAL = new EmployeeDAL.Class1();  
  29.                 return objDAL.DeleteData(Id);  
  30.             } catch (Exception ex) {  
  31.                 throw ex;  
  32.             }  
  33.         }  
  34.         public DataTable BindGrid() {  
  35.             try {  
  36.                 EmployeeDAL.Class1 objDAL = new EmployeeDAL.Class1();  
  37.                 return objDAL.BindGrid();  
  38.             } catch (Exception ex) {  
  39.                 throw ex;  
  40.             }  
  41.         }  
  42.         public DataTable GetById(int Id) {  
  43.             try {  
  44.                 EmployeeDAL.Class1 objDAL = new EmployeeDAL.Class1();  
  45.                 return objDAL.GetById(Id);  
  46.             } catch (Exception ex) {  
  47.                 throw ex;  
  48.             }  
  49.         }  
  50.     }  
  51. }  
Now right click on project in solution explorer and  add a webform and name it to webform1.aspx.

On design side write this code: 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication14.WebForm2" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <head runat="server">  
  6.         <title></title>  
  7.     </head>  
  8.     <body>  
  9.         <form id="form1" runat="server">  
  10.             <div>  
  11.                 <table>  
  12.                     <tr>  
  13.                         <td>  
  14.                             <asp:Label ID="lblName" runat="server" Text="Enter Name"></asp:Label>  
  15.                         </td>  
  16.                         <td>  
  17.                             <asp:TextBox ID="txtName" runat="server"></asp:TextBox>  
  18.                         </td>  
  19.                     </tr>  
  20.                     <tr>  
  21.                         <td>  
  22.                             <asp:Label ID="lblAge" runat="server" Text="Enter Age"></asp:Label>  
  23.                         </td>  
  24.                         <td>  
  25.                             <asp:TextBox ID="txtAge" runat="server"></asp:TextBox>  
  26.                         </td>  
  27.                     </tr>  
  28.                     <tr>  
  29.                         <td>  
  30.                             <asp:Label ID="lblAddress" runat="server" Text="Enter Address"></asp:Label>  
  31.                         </td>  
  32.                         <td>  
  33.                             <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>  
  34.                         </td>  
  35.                     </tr>  
  36.                     <tr>  
  37.                         <td>  
  38.                             <asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />  
  39.                         </td>  
  40.                     </tr>  
  41.                     <tr>  
  42.                         <td>  
  43.                             <asp:GridView ID="gvDisplay" runat="server" AutoGenerateColumns="false"  
  44.                                 AllowPaging="true" PageSize="2"  
  45.                                 onrowcommand="gvDisplay_RowCommand" onrowdeleting="gvDisplay_RowDeleting"  
  46.                                 onselectedindexchanging="gvDisplay_SelectedIndexChanging"  
  47.                                 onpageindexchanging="gvDisplay_PageIndexChanging">  
  48.                                 <Columns>  
  49.                                     <asp:BoundField DataField="Id" HeaderText="Id" />  
  50.                                     <asp:BoundField DataField="Name" HeaderText="Name" />  
  51.                                     <asp:BoundField DataField="Address" HeaderText="Address" />  
  52.                                     <asp:BoundField DataField="Age" HeaderText="Age" />  
  53.                                     <asp:CommandField ButtonType="Button" SelectText="Edit" ShowSelectButton="True" />  
  54.                                     <asp:TemplateField>  
  55.                                         <ItemTemplate>  
  56.                                             <asp:Button runat="server" ID="btnDelete" OnClientClick="return confirm('Are you sure,you want to delete this record ?');" Text="Delete" CommandArgument='  
  57.                                                 <%# Eval("Id") %>' CommandName="Delete" />  
  58.                                             </ItemTemplate>  
  59.                                         </asp:TemplateField>  
  60.                                     </Columns>  
  61.                                 </asp:GridView>  
  62.                             </td>  
  63.                         </tr>  
  64.                     </table>  
  65.                 </div>  
  66.             </form>  
  67.         </body>  
  68.     </html>  
In cs side write the following code,add reference of EmployeeDAL,EmployeeSchema and EmployeeBAL in the project.
  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 EmployeeDAL;  
  8. using EmployeeBAL;  
  9. using System.Data;  
  10. namespace WebApplication14 {  
  11.     public partial class WebForm2: System.Web.UI.Page {  
  12.         DataTable dt;  
  13.         protected void Page_Load(object sender, EventArgs e) {  
  14.             if (!Page.IsPostBack) {  
  15.                 gvDisplay.Visible = false;  
  16.             }  
  17.         }  
  18.         protected void btnSubmit_Click(object sender, EventArgs e) {  
  19.             try {  
  20.                 if (btnSubmit.Text == "Submit") {  
  21.                     InsertData();  
  22.                 } else if (btnSubmit.Text == "Update") {  
  23.                     int Id = int.Parse(gvDisplay.Rows[gvDisplay.SelectedIndex].Cells[0].Text);  
  24.                     UpdateData(Id);  
  25.                 }  
  26.             } catch (Exception ex) {  
  27.                 throw ex;  
  28.             }  
  29.         }  
  30.         public void UpdateData(int Id) {  
  31.             EmployeeSchema.Class1 objSchema = new EmployeeSchema.Class1();  
  32.             objSchema.Name = txtName.Text;  
  33.             objSchema.Address = txtAddress.Text;  
  34.             objSchema.Age = Convert.ToInt32(txtAge.Text);  
  35.             EmployeeBAL.Class1 objBAL = new EmployeeBAL.Class1();  
  36.             int result = objBAL.Update(objSchema, Id);  
  37.             if (result > 0) {  
  38.                 ScriptManager.RegisterStartupScript(thisthis.GetType(), "alert""alert('Data Updated Successfully')"true);  
  39.             }  
  40.             btnSubmit.Text = "Submit";  
  41.             BindGrid();  
  42.             Clear();  
  43.         }  
  44.         public void InsertData() {  
  45.             EmployeeSchema.Class1 objSchema = new EmployeeSchema.Class1();  
  46.             objSchema.Name = txtName.Text;  
  47.             objSchema.Address = txtAddress.Text;  
  48.             objSchema.Age = Convert.ToInt32(txtAge.Text);  
  49.             EmployeeBAL.Class1 objBAL = new EmployeeBAL.Class1();  
  50.             int result = objBAL.Insert(objSchema);  
  51.             if (result > 0) {  
  52.                 ScriptManager.RegisterStartupScript(thisthis.GetType(), "alert""alert('Data Saved Successfully')"true);  
  53.             }  
  54.             BindGrid();  
  55.             Clear();  
  56.         }  
  57.         private void Clear() {  
  58.             txtName.Text = "";  
  59.             txtAge.Text = "";  
  60.             txtAddress.Text = "";  
  61.         }  
  62.         private void BindGrid() {  
  63.             try {  
  64.                 EmployeeBAL.Class1 objBal = new EmployeeBAL.Class1();  
  65.                 gvDisplay.Columns[0].Visible = true;  
  66.                 gvDisplay.DataSource = objBal.BindGrid();  
  67.                 gvDisplay.DataBind();  
  68.                 gvDisplay.Columns[0].Visible = false;  
  69.                 gvDisplay.Visible = true;  
  70.             } catch (Exception ex) {  
  71.                 throw ex;  
  72.             }  
  73.         }  
  74.         protected void gvDisplay_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) {  
  75.             try {  
  76.                 EmployeeBAL.Class1 objBAL = new EmployeeBAL.Class1();  
  77.                 int Id = int.Parse(gvDisplay.Rows[e.NewSelectedIndex].Cells[0].Text);  
  78.                 dt = new DataTable();  
  79.                 dt = objBAL.GetById(Id);  
  80.                 if (dt.Rows.Count > 0) {  
  81.                     txtName.Text = dt.Rows[0]["Name"].ToString();  
  82.                     txtAddress.Text = dt.Rows[0]["Address"].ToString();  
  83.                     txtAge.Text = dt.Rows[0]["Age"].ToString();  
  84.                     btnSubmit.Text = "Update";  
  85.                 }  
  86.             } catch (Exception ex) {  
  87.                 throw ex;  
  88.             }  
  89.         }  
  90.         protected void gvDisplay_RowCommand(object sender, GridViewCommandEventArgs e) {  
  91.             if (e.CommandName.Equals("Delete")) {  
  92.                 int Id = int.Parse(e.CommandArgument.ToString());  
  93.                 DeleteRecord(Id);  
  94.             }  
  95.         }  
  96.         private void DeleteRecord(int Id) {  
  97.             try {  
  98.                 EmployeeBAL.Class1 objBAL = new EmployeeBAL.Class1();  
  99.                 int Result = objBAL.Delete(Id);  
  100.                 if (Result > 0) {  
  101.                     ScriptManager.RegisterStartupScript(thisthis.GetType(), "alert""alert('Data Deleted Successfully')"true);  
  102.                 }  
  103.                 BindGrid();  
  104.                 Clear();  
  105.             } catch (Exception ex) {  
  106.                 throw ex;  
  107.             }  
  108.         }  
  109.         protected void gvDisplay_RowDeleting(object sender, GridViewDeleteEventArgs e) {}  
  110.         protected void gvDisplay_PageIndexChanging(object sender, GridViewPageEventArgs e) {  
  111.             gvDisplay.PageIndex = e.NewPageIndex;  
  112.             BindGrid();  
  113.         }  
  114.     }  
  115. }   
Implement this.I will go on with more details in further blogs.Do not worry. 
Ebook Download
View all
Learn
View all