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:
- Presentation Layer
- Data Access Layer
- 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.
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.
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
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.
- Create TABLE Laptops
- (
- Productid int identity(1,1),
- ProductName varchar(50),
- ProductCost money,
- ProductDescription varchar(500),
- Productimage nvarchar(500);
- );
Insert some values into Laptops Table after that create Stored Procedure and name is ss_Laptops.
- Create procedure [dbo].[ss_Laptops]
- as
- begin
- select * from Laptops
- end
Now create the database connection in DAL.cs class file
- using System;
- using System.Xml;
- using System.Data.SqlClient;
- using System.Collections;
- using System.Configuration;
- using System.Data;
-
- public class DAL
- {
- static ConnectionStringSettings wwl = ConfigurationManager.ConnectionStrings["WWl"];
- public static SqlConnection con = new SqlConnection(wwl.ConnectionString);
- public DAL()
- {
- if (con.State != ConnectionState.Open)
- {
- con.Open();
- }
- }
-
- public static DataSet ExecuteDataSet(string SPName)
- {
- try
- {
- DataSet dsresult = new DataSet();
- using (SqlConnection connect = new SqlConnection(wwl.ConnectionString))
- {
- connect.Open();
- SqlCommand cmd = new SqlCommand(SPName.Trim(), connect);
- cmd.CommandTimeout = 0;
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- da.Fill(dsresult);
- return dsresult;
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- { }
- }
- }
Now goto DAO.cs file and create the properties like below.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
- public class DAO
- {
- public DAO()
- {
- }
- private int _pid;
- private string _pname;
- private decimal _pcost;
- private string _pdec;
- private string _pimage;
- public int Productid
- {
- get { return _pid; }
- set { _pid = value; }
- }
- public string ProductName
- {
- get { return _pname; }
- set { _pname = value; }
- }
- public decimal ProductCost
- {
- get { return _pcost; }
- set { _pcost = value; }
- }
- public string ProductDescription
- {
- get { return _pdec; }
- set { _pdec = value; }
- }
- public string Productimage
- {
- get { return _pimage; }
- set { _pimage = value; }
- }
- }
Now go to BL.cs file and write the business logic as show below.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Data;
- using System.Configuration;
-
- public class BL
- {
- DAL dal = new DAL();
- DAO dao = new DAO();
- public BL()
- {
-
-
-
- }
- public DataSet disProduct()
- {
- string sStoreProcedure = "ss_Laptops";
- try
- {
- return DAL.ExecuteDataSet(sStoreProcedure);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }
Finally design the Details.aspx page
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
-
- <asp:DataList ID="DataList2" runat="server"
- onitemcommand="DataList2_ItemCommand" RepeatColumns="5" Width="927px">
- <ItemTemplate>
- <asp:Panel ID="Panel1" runat="server">
- <table height=150>
- <tr>
- <td width=”75%” align=”center” style=”color: #FF0000; font-weight: bold”>
- <asp:ImageButton ID="Image1" runat="server" Width="100px" Height="70px" ImageUrl='<%#Eval("Productimage")%>' CommandName="ViewDetails" CommandArgument='<%#Eval("Productid") %>' />
- </td>
- </tr>
- <tr>
- <td>
- <span style="color:Black;font-weight:bold;">Product Name</span>
- <asp:Label ID="lb1" runat ="server" Text='<%#Eval("ProductName") %>' ></asp:Label>
- </td>
- </tr>
- <tr>
- <td>
- <span style="color:Black;font-weight:bold;">Product Details</span><br />
- <asp:Label ID="lbl2" runat="server" Text='<%#Eval("ProductDescription") %>' ></asp:Label>
- </td>
- </tr>
- <tr>
- <td>
- <span style ="color:Black;font-weight:bold">Product Cost</span>
- <asp:Label ID="lbl3" runat ="server" Text='<%#Eval("ProductCost") %>' ></asp:Label>
- </td>
- </tr>
- <tr>
- <td>
- <%--<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>--%>
- <asp:LinkButton ID="LinkButton2" runat="server" CommandName="ViewDetails" CommandArgument='<%#Eval("Productid") %>' OnClick="LinkButton_Click">ViewDeatils</asp:LinkButton>
- </td>
- </tr>
- </table>
- </asp:Panel>
- </ItemTemplate>
- </asp:DataList>
-
- </div>
-
-
- </form>
-
- </html>
Click on F7 button and go to code behind that is Default.cs file and write the code.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
-
- public partial class _Default : System.Web.UI.Page
- {
- DataSet ds = new DataSet();
- DataTable dt = new DataTable();
- BL blBL = new BL();
-
-
- protected void Page_Load(object sender, EventArgs e)
- {
- if(!IsPostBack==true)
- {
- GetData();
- }
- }
- public void GetData()
- {
-
- DataSet result = blBL.disProduct();
- DataTable dt = result.Tables[0];
- DataList2.DataSource = result.Tables[0].DefaultView;
- DataList2.DataBind();
- DataTable td = dt;
- Session["dt"]=td;
-
- }
- protected void LinkButton_Click(Object sender, EventArgs e)
- {
- detashow();
- }
- public void detashow()
- {
- }
- protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
- {
- if (e.CommandName == "ViewDetails")
- {
- Response.Redirect("ViewDetails.aspx?id=" + e.CommandArgument.ToString());
- }
- }
- }
Now go to the second page that is ViewDetails.aspx page and write the code:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ViewDetails.aspx.cs" Inherits="Home_ViewDetails" %>
-
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title></title>
- <style type=”text/css”>
- .style3
- {
- width: 100%;
- background-color: #FFCCCC;
- }
- .style4
- {
- width: 25px;
- height: 23px;
- }
- .style5
- {
- height: 23px;
- width: 324px;
- }
- .style6
- {
- height: 23px;
- width: 207px;
- }
- </style>
- </head>
- <body>
- <form id="form2" runat="server">
- <div>
- <table class="style3">
- <tr>
- <td class="style6">
- </td>
- <td class="style5">
- <table class=”style1″>
- <tr>
- <td class=”style2″>
- <asp:Image ID="Image1" runat="server" Width="301px" Height="209px"
- style="margin-right: 130px" />
- </td>
- </tr>
- <tr>
- <td style=”color: #0000FF; font-weight: 700″ >
- <span style=”color: Black; font-weight: bold;“>Modal:</span><br />
- <asp:Literal ID="Literal1" runat="server"></asp:Literal>
- </td>
- </tr>
-
- <tr>
- <td style=”font-weight: 700; color: #009933″ >
- <span style=”color: Black; font-weight: bold;“>ProductDetails:</span><br />
- <asp:Literal ID="Literal2" runat="server"></asp:Literal>
- </td>
- </tr>
- <tr>
- <td style=”font-weight: 700; color: #FF0000″ >
- <span style=”color: Black; font-weight: bold;“>Price:</span><br />
- <asp:Literal ID="Literal3" runat="server"></asp:Literal>
-
- </td>
- </tr>
- </table>
- <td class="style4">
- </td>
- </tr>
- <tr>
- <td class="style6">
- </td>
- <td class="style5">
- <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Home/Default.aspx">Goto Home Page</asp:HyperLink>
- <td class="style4">
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
Now to go code behind ViewDetails.cs file and write the code.
- using System;
- using System.Collections.Generic;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
- using System.IO;
- using System.Xml;
-
-
- public partial class Home_ViewDetails : System.Web.UI.Page
- {
- static ConnectionStringSettings wwl = ConfigurationManager.ConnectionStrings["WWL"];
- public static SqlConnection con = new SqlConnection(wwl.ConnectionString);
- protected void Page_Load(object sender, EventArgs e)
- {
- GetData();
-
- }
- public void GetData()
- {
- DataTable dt = (DataTable)Session["dt"];
- Image1.ImageUrl=dt.Rows[0][4].ToString();
- Literal1.Text=dt.Rows[0][1].ToString();
- Literal2.Text = dt.Rows[0][3].ToString();
- Literal3.Text=dt.Rows[0][2].ToString();
-
- dt.WriteXml("D:/A.xls");
- }
- }