Three Tier Architecture In ASP.NET

Open Visual Studio and create a new ASP.NET Web Application, then right click on Solution and Add New Project. After that add Class Library. Give name DAL(Data Access Layer) and write the following code in Data Access Layer. 
  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.   
  9. namespace DAL  
  10. {  
  11.    public class Class1  
  12.    {  
  13.       SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());  
  14.       SqlCommand cmd;  
  15.       DataTable dt;  
  16.   
  17.       public DataTable Person(int Id)  
  18.       {  
  19.          using (cmd = new SqlCommand("Three_Tier_Test", con))  
  20.          {  
  21.             try  
  22.             {  
  23.                cmd.CommandType = CommandType.StoredProcedure;  
  24.                cmd.Parameters.AddWithValue("@Id", Id);  
  25.                if(con.State.Equals(ConnectionState.Closed))  
  26.                con.Open();  
  27.                SqlDataAdapter da=new SqlDataAdapter(cmd);  
  28.                dt=new DataTable();  
  29.                da.Fill(dt);  
  30.                return dt;  
  31.   
  32.             }  
  33.             catch(Exception ex)  
  34.             {  
  35.                throw ex;  
  36.             }  
  37.          }  
  38.       }  
  39.    }  
  40. }  
Build this Layer

Now in the same way add BAL (Business Access Layer), this is also a Class Library. Write the following code in BAL:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6.   
  7. namespace BAL  
  8. {  
  9. public class Class1  
  10. {  
  11. public DataTable Persons(int Id)  
  12. {  
  13. try  
  14. {  
  15. DAL.Class1 objDAL = new DAL.Class1();  
  16. return objDAL.Person(Id);  
  17. }  
  18. catch (Exception ex)  
  19. {  
  20. throw ex;  
  21. }  
  22. }  
  23. }  
  24. }  
Now Add a WebForm and Add a button, GridView and Label and a Textbox on it.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication10.WebForm1" %>  
  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="lblId" runat="server" Text="User Id"></asp:Label>  
  15.                         </td>  
  16.                         <td>  
  17.                             <asp:TextBox ID="txtId" runat="server"></asp:TextBox>  
  18.                         </td>  
  19.                     </tr>  
  20.                     <tr>  
  21.                         <td>  
  22.                             <asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />  
  23.                         </td>  
  24.                     </tr>  
  25.                     <tr>  
  26.                         <td>  
  27.                             <asp:GridView ID="gvDisplay" runat="server" AutoGenerateColumns="true"></asp:GridView>  
  28.                         </td>  
  29.                     </tr>  
  30.                 </table>  
  31.             </div>  
  32.         </form>  
  33.     </body>  
  34. </html>  
Above is code behind on cs, after that write the following 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.   
  8. namespace WebApplication10  
  9. {  
  10.    public partial class WebForm1 : System.Web.UI.Page  
  11.    {  
  12.       protected void Page_Load(object sender, EventArgs e)  
  13.       {  
  14.          if (!Page.IsPostBack)  
  15.          {  
  16.             gvDisplay.Visible = false;  
  17.          }  
  18.       }  
  19.   
  20.       protected void btnSubmit_Click(object sender, EventArgs e)  
  21.       {  
  22.          BAL.Class1 objBal = new BAL.Class1();  
  23.          gvDisplay.DataSource= objBal.Persons(Convert.ToInt32(txtId.Text));  
  24.          gvDisplay.DataBind();  
  25.          gvDisplay.Visible = true;  
  26.       }  
  27.    }  
  28. }  
Run the application. Do not forget to add reference of BAL and DAL in the application. Also Add Reference DAL in BAL as from BAL we are calling DAL method. This is just the beginning, I will expand it to CRUD Operations and interview questions very soon.
Ebook Download
View all
Learn
View all