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.     static ConnectionStringSettings wwl =     ConfigurationManager.ConnectionStrings["WWl"];    
  11.     public static SqlConnection con = new SqlConnection(wwl.ConnectionString);    
  12.     public DAL()    
  13.     {    
  14.         if (con.State != ConnectionState.Open)    
  15.         {    
  16.             con.Open();    
  17.         }    
  18.     }    
  19.     
  20.     public static DataSet ExecuteDataSet(string SPName)    
  21.     {    
  22.         try    
  23.         {  
  24.             DataSet dsresult = new DataSet();    
  25.             using (SqlConnection connect = new SqlConnection(wwl.ConnectionString))    
  26.             {    
  27.                 connect.Open();    
  28.                 SqlCommand cmd = new SqlCommand(SPName.Trim(), connect);    
  29.                 cmd.CommandTimeout = 0;    
  30.                 SqlDataAdapter da = new SqlDataAdapter(cmd);    
  31.                 da.Fill(dsresult);    
  32.                 return dsresult;    
  33.             }  
  34.         }    
  35.         catch (Exception ex)    
  36.         {    
  37.             throw ex;    
  38.         }    
  39.         finally    
  40.         { }    
  41.     }    
  42. } 

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.     private int _pid;    
  12.     private string _pname;    
  13.     private decimal _pcost;    
  14.     private string _pdec;    
  15.     private string _pimage;    
  16.     public int Productid    
  17.     {    
  18.         get { return _pid; }    
  19.         set { _pid = value; }    
  20.     }    
  21.     public string ProductName    
  22.     {    
  23.         get { return _pname; }    
  24.         set { _pname = value; }    
  25.     }    
  26.     public decimal ProductCost    
  27.     {    
  28.         get { return _pcost; }    
  29.         set { _pcost = value; }    
  30.     }    
  31.     public string ProductDescription    
  32.     {    
  33.         get { return _pdec; }    
  34.         set { _pdec = value; }    
  35.     }    
  36.     public string Productimage    
  37.     {    
  38.         get { return _pimage; }    
  39.         set { _pimage = value; }    
  40.     }  
  41. } 

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.         string sStoreProcedure = "ss_Laptops";    
  21.         try    
  22.         {    
  23.             return DAL.ExecuteDataSet(sStoreProcedure);    
  24.         }    
  25.         catch (Exception ex)    
  26.         {    
  27.             throw ex;    
  28.         }    
  29.     }    
  30. } 

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.     protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)    
  42.     {    
  43.         if (e.CommandName == "ViewDetails")    
  44.         {    
  45.             Response.Redirect("ViewDetails.aspx?id=" + e.CommandArgument.ToString());    
  46.         }    
  47.     }    
  48. } 

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.     public void GetData()    
  23.     {  
  24.         DataTable dt = (DataTable)Session["dt"];    
  25.         Image1.ImageUrl=dt.Rows[0][4].ToString();    
  26.         Literal1.Text=dt.Rows[0][1].ToString();    
  27.         Literal2.Text = dt.Rows[0][3].ToString();    
  28.         Literal3.Text=dt.Rows[0][2].ToString();    
  29.     
  30.         dt.WriteXml("D:/A.xls");    
  31.     }    
  32. } 


Similar Articles