Three-Tier Architecture In ASP.NET With Example

In this tutorial, I will explain how to create three-tier architecture for projects in Asp.net. In three tier architecture we have three layers. They are:

  1. Presentation Layer
  2. Data Access Layer
  3. Business Logic Layer.

What is a Layer?

A layer is reusable portion of a code. In three-tier architecture we are creating three layers and reusing the code as per our requirement.

What is Data Access Layer ?

Data Access Layer is used to connect the Business Logic Layer to Data Base  to perform the CRUD operations like Insert , Delete, Update, Select and so on.

What is Business Logic Layer?

Business Logic Layer contains the business logic.

What is Presentation Layer?

Presentation Layer contains .aspx  pages means, we present the controls in frontend like textbox, dropdown list etc.

Introduction to Project

In this project, I will display the laptops in Default.aspx page. The page contains the DataList control to display the product details. DataList Control Displays the Images, Product Name, Product Details, Product Cost, ViewDetails as shown below image.

ASP.NET
Whenever I click on View Details Link Button then it redirects the ViewDetails.aspx page, like the below image. And in ViewDetails page, I display the all information of the laptop with “Go to Home Page” Link button.

ASP.NET

Data Flow

If  we open the website Default.aspx page then it will load in browser. Here Page Load Event will fire and get the data from Data base and display the data list in Default.aspx page and store the data in Session[“id”].  If we click on View Details link button then all values of Session[“id”] will store in dt in ViewDetails.aspx page as Datatable and pass the id to viewdetails.aspx page to display the selected data list values in viewdetails.aspx page.

File Structure

ASP.NET

The above image shows the three tier architecture with separate folders.  App_Code folder has three files BL, DAL, DAO files and each folder contains the .cs files. The BL file contains the  BL.cs file class used to create the business logic. And DAL fil contains the DAL.cs class used to create the connection between BL to Data Base . DAO file contains the properties DAO.cs class. Home folder contains .aspx pages like Default.aspx and ViewDetails.aspx page.

Source Code

In this tutorial I will show the source code.

Database Creation

First create the table in sql server and name it as Laptops and insert some values into the table  after creating the stored procedure and name it as ss_Laptops.

  1. Create TABLE Laptops  
  2.    (  
  3. Productid int identity(1,1),  
  4. ProductName varchar(50),  
  5. ProductCost money,  
  6. ProductDescription varchar(500),  
  7. Productimage nvarchar(500);  
  8. );  

Insert some values into Laptops Table after that create Stored Procedure and name is  ss_Laptops.

  1. Create procedure [dbo].[ss_Laptops]  
  2. as  
  3. begin  
  4. select * from Laptops  
  5. end  

Now create the database connection in DAL.cs class file

  1. using System;  
  2. using System.Xml;  
  3. using System.Data.SqlClient;  
  4. using System.Collections;  
  5. using System.Configuration;  
  6. using System.Data;  
  7.   
  8.     public class DAL  
  9.     {  
  10.   
  11.         static ConnectionStringSettings wwl =     ConfigurationManager.ConnectionStrings["WWl"];  
  12.         public static SqlConnection con = new SqlConnection(wwl.ConnectionString);  
  13.         public DAL()  
  14.         {  
  15.             if (con.State != ConnectionState.Open)  
  16.             {  
  17.                 con.Open();  
  18.             }  
  19.         }  
  20.   
  21.         public static DataSet ExecuteDataSet(string SPName)  
  22.         {  
  23.             try  
  24.             {  
  25.   
  26.                 DataSet dsresult = new DataSet();  
  27.                 using (SqlConnection connect = new SqlConnection(wwl.ConnectionString))  
  28.                 {  
  29.                     connect.Open();  
  30.                     SqlCommand cmd = new SqlCommand(SPName.Trim(), connect);  
  31.                     cmd.CommandTimeout = 0;  
  32.                     SqlDataAdapter da = new SqlDataAdapter(cmd);  
  33.                     da.Fill(dsresult);  
  34.                     return dsresult;  
  35.   
  36.                 }  
  37.   
  38.             }  
  39.             catch (Exception ex)  
  40.             {  
  41.                 throw ex;  
  42.             }  
  43.             finally  
  44.             { }  
  45.         }  
  46.     }  

Now goto DAO.cs file and create the properties like below.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. public class DAO  
  7. {  
  8.     public DAO()  
  9.     {  
  10.           
  11.   
  12.     }  
  13.     private int _pid;  
  14.     private string _pname;  
  15.     private decimal _pcost;  
  16.     private string _pdec;  
  17.     private string _pimage;  
  18.     public int Productid  
  19.     {  
  20.         get { return _pid; }  
  21.         set { _pid = value; }  
  22.     }  
  23.     public string ProductName  
  24.     {  
  25.         get { return _pname; }  
  26.         set { _pname = value; }  
  27.     }  
  28.     public decimal ProductCost  
  29.     {  
  30.         get { return _pcost; }  
  31.         set { _pcost = value; }  
  32.     }  
  33.     public string ProductDescription  
  34.     {  
  35.         get { return _pdec; }  
  36.         set { _pdec = value; }  
  37.     }  
  38.     public string Productimage  
  39.     {  
  40.         get { return _pimage; }  
  41.         set { _pimage = value; }  
  42.     }  
  43.   
  44. }  

Now go to BL.cs file and write the business logic as show below.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Data;  
  6. using System.Configuration;  
  7.   
  8. public class BL  
  9. {  
  10.     DAL dal = new DAL();  
  11.     DAO dao = new DAO();  
  12.     public BL()  
  13.     {  
  14.         //  
  15.         // TODO: Add constructor logic here  
  16.         //  
  17.     }  
  18.     public DataSet disProduct()  
  19.     {  
  20.          
  21.         string sStoreProcedure = "ss_Laptops";  
  22.         try  
  23.         {  
  24.             return DAL.ExecuteDataSet(sStoreProcedure);  
  25.         }  
  26.         catch (Exception ex)  
  27.         {  
  28.             throw ex;  
  29.         }  
  30.     }  
  31. }  

Finally design the Details.aspx page

  1.   <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3.   
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head id="Head1" runat="server">  
  6.     <title></title>  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.     <div>  
  11.       
  12.     <asp:DataList ID="DataList2" runat="server"   
  13.         onitemcommand="DataList2_ItemCommand" RepeatColumns="5" Width="927px">  
  14.           <ItemTemplate>  
  15.             <asp:Panel ID="Panel1" runat="server">  
  16.               <table height=150>  
  17.                <tr>  
  18.                <td width=”75%” align=”center” style=”color: #FF0000; font-weight: bold”>  
  19.                 <asp:ImageButton ID="Image1" runat="server" Width="100px" Height="70px" ImageUrl='<%#Eval("Productimage")%>' CommandName="ViewDetails" CommandArgument='<%#Eval("Productid") %>' />  
  20.                </td>  
  21.                </tr>  
  22.                <tr>  
  23.                <td>  
  24.                <span style="color:Black;font-weight:bold;">Product Name</span>  
  25.                <asp:Label ID="lb1" runat ="server" Text='<%#Eval("ProductName") %>' ></asp:Label>  
  26.                </td>  
  27.                </tr>  
  28.                <tr>  
  29.                <td>  
  30.                 <span style="color:Black;font-weight:bold;">Product Details</span><br />  
  31.                 <asp:Label ID="lbl2" runat="server" Text='<%#Eval("ProductDescription") %>' ></asp:Label>  
  32.                </td>  
  33.                </tr>  
  34.                <tr>  
  35.                <td>  
  36.                <span style ="color:Black;font-weight:bold">Product Cost</span>  
  37.                  <asp:Label ID="lbl3" runat ="server" Text='<%#Eval("ProductCost") %>' ></asp:Label>  
  38.                </td>  
  39.                </tr>  
  40.                <tr>  
  41.                <td>  
  42.                 <%--<asp:LinkButton ID="LinkButton1" runat="server" Font-Underline="False"style="font-weight:700; color:Black" CommandName="ViewDetails" CommandArgument='<%#Eval(Productid) %>'  BackColor="#FF9933">ViewDeatils</asp:LinkButton>--%>  
  43.                <asp:LinkButton ID="LinkButton2" runat="server" CommandName="ViewDetails" CommandArgument='<%#Eval("Productid") %>' OnClick="LinkButton_Click">ViewDeatils</asp:LinkButton>  
  44.                </td>  
  45.                </tr>  
  46.         </table>  
  47.           </asp:Panel>  
  48.          </ItemTemplate>  
  49.     </asp:DataList>  
  50.           
  51.     </div>  
  52.   
  53.           
  54.     </form>  
  55.    
  56. </html>  

Click on F7 button and go to code behind that is Default.cs file and write the 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;  
  8.   
  9. public partial class _Default : System.Web.UI.Page   
  10. {   
  11.         DataSet ds = new DataSet();  
  12.         DataTable dt = new DataTable();  
  13.         BL blBL = new BL();  
  14.       
  15.         
  16.     protected void Page_Load(object sender, EventArgs e)  
  17.     {  
  18.         if(!IsPostBack==true)  
  19.         {  
  20.         GetData();  
  21.          }  
  22.     }  
  23.     public void GetData()  
  24.     {  
  25.   
  26.         DataSet result = blBL.disProduct();  
  27.         DataTable dt = result.Tables[0];  
  28.         DataList2.DataSource = result.Tables[0].DefaultView;  
  29.         DataList2.DataBind();  
  30.         DataTable td = dt;  
  31.         Session["dt"]=td;  
  32.   
  33.     }  
  34.     protected void LinkButton_Click(Object sender, EventArgs e)   
  35.     {  
  36.         detashow();  
  37.     }  
  38.     public void detashow()  
  39.     {   
  40.   
  41.     }  
  42.   
  43.     protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)  
  44.     {  
  45.         if (e.CommandName == "ViewDetails")  
  46.         {  
  47.             Response.Redirect("ViewDetails.aspx?id=" + e.CommandArgument.ToString());  
  48.         }  
  49.     }  
  50. }  

Now go to the second page that is ViewDetails.aspx page and write the code:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ViewDetails.aspx.cs" Inherits="Home_ViewDetails" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head id="Head1" runat="server">  
  7.     <title></title>  
  8.     <style type=”text/css”>  
  9.         .style3  
  10.         {  
  11.             width: 100%;  
  12.             background-color: #FFCCCC;  
  13.         }  
  14.         .style4  
  15.         {  
  16.             width: 25px;  
  17.             height: 23px;  
  18.         }  
  19.         .style5  
  20.         {  
  21.             height: 23px;  
  22.             width: 324px;  
  23.         }  
  24.         .style6  
  25.         {  
  26.             height: 23px;  
  27.             width: 207px;  
  28.         }  
  29.     </style>  
  30. </head>  
  31. <body>  
  32.     <form id="form2" runat="server">  
  33.     <div>  
  34.         <table class="style3">  
  35.              <tr>  
  36.                  <td class="style6">  
  37.                      </td>  
  38.                  <td class="style5">  
  39.                            <table class=”style1″>  
  40.       <tr>  
  41.         <td class=”style2″>  
  42.          <asp:Image ID="Image1" runat="server" Width="301px" Height="209px"   
  43.                 style="margin-right: 130px" />  
  44.           </td>  
  45.         </tr>  
  46.        <tr>  
  47.        <td style=”color: #0000FF; font-weight: 700″ >  
  48.          <span style=”color: Black; font-weight: bold;“>Modal:</span><br />  
  49.          <asp:Literal ID="Literal1" runat="server"></asp:Literal>  
  50.     </td>  
  51.     </tr>  
  52.   
  53.     <tr>  
  54.    <td style=”font-weight: 700; color: #009933″ >  
  55.       <span style=”color: Black; font-weight: bold;“>ProductDetails:</span><br />  
  56.      <asp:Literal ID="Literal2" runat="server"></asp:Literal>     
  57.     </td>  
  58.     </tr>  
  59.     <tr>  
  60.     <td style=”font-weight: 700; color: #FF0000″ >  
  61.          <span style=”color: Black; font-weight: bold;“>Price:</span><br />  
  62.          <asp:Literal ID="Literal3" runat="server"></asp:Literal>  
  63.            
  64.     </td>  
  65.     </tr>  
  66.       </table>  
  67.                  <td class="style4">  
  68.                      </td>  
  69.              </tr>  
  70.              <tr>  
  71.                  <td class="style6">  
  72.                       </td>  
  73.                  <td class="style5">  
  74.                      <asp:HyperLink ID="HyperLink1"  runat="server" NavigateUrl="~/Home/Default.aspx">Goto Home Page</asp:HyperLink>  
  75.                  <td class="style4">  
  76.                       </td>  
  77.              </tr>  
  78.          </table>  
  79.     </div>      
  80.     </form>     
  81.     </body>  
  82. </html>  

Now to go code behind ViewDetails.cs file and write the code.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Web;  
  4. using System.Web.UI;  
  5. using System.Web.UI.WebControls;  
  6. using System.Data;  
  7. using System.Data.SqlClient;  
  8. using System.Configuration;  
  9. using System.IO;  
  10. using System.Xml;  
  11.   
  12.   
  13. public partial class Home_ViewDetails : System.Web.UI.Page  
  14. {  
  15.     static ConnectionStringSettings wwl = ConfigurationManager.ConnectionStrings["WWL"];  
  16.     public static SqlConnection con = new SqlConnection(wwl.ConnectionString);  
  17.     protected void Page_Load(object sender, EventArgs e)  
  18.     {  
  19.         GetData();  
  20.           
  21.     }  
  22.   
  23.     public void GetData()  
  24.     {  
  25.    
  26.         DataTable dt = (DataTable)Session["dt"];  
  27.         Image1.ImageUrl=dt.Rows[0][4].ToString();  
  28.         Literal1.Text=dt.Rows[0][1].ToString();  
  29.         Literal2.Text = dt.Rows[0][3].ToString();  
  30.         Literal3.Text=dt.Rows[0][2].ToString();  
  31.   
  32.         dt.WriteXml("D:/A.xls");  
  33.     }  
  34. }