How to create a website using 3 tier architecture
In this article i am going to discuss how to implement a website using 3 tier architecture.
This architecture consists of three layers.They are :
This layer contains the user interface of the websites i.e the visible part of a website.
Business Logic Layer:
This layer acts as a mediator between Presentation layer and Data Access layer.It transfers Data between the two layers.The main aim of this layer is to protect data from being accessed by end user.
Data Access Layer:
This layer deals with the database.we write stored procedures,queries in this layer.
In this example I am going to explain how to implement 3 tier architechture to display
Data in a gridview using Northwind database.For this website you need to have
Visual studio 2005 and sql server 2000 to be installed in your system.so here we go
First open visual studio 2005 and create a project with the name TESTDAL and choose option for class library as shown in picture
Rename the class1 present in the project with AdminDAL and save the project.
In the same way create a project with the name TESTBL and rename the class file to Managegridview and save the project as shown below
Now, create a website with name TESTWEB in your visual studio as shown in figure
In the TESTWEB wesite go to solution explorer.Right click on the solution explorer and choose option add .In the add option you will have a option "Existing Project" ,choose that option .You u can see that in this figure
After you choose Existing project you will get a browse option ,choose the location of your project TESTBL ,in this example it is in D:/ .Choose the option high lighted in the figure.
After you add the project,TESTBL will be displayed in your website .Repeat the same procedure and add TESTDAL project in your website.After you add the two projects TESTBL and TESTDAL ,the solution explorer will be as shown below
Now, in the AdminDAL of TESTDAL add this code
public class AdminDAL
static SqlConnection conn;
static bool connected;
private static void Connect()
int count = 0;
conn = new SqlConnection("User ID=sa; Initial Catalog=Northwind;Integrated Security=SSPI;Persist Security Info=False; Data Source=localhost");
catch (Exception ex)
catch (SqlException ex)
private static void closeconnection()
public static ICollection GetData()
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("", conn);
string query = "SELECT Title,FirstName,LastName from Employees";
da.SelectCommand = new SqlCommand(query, conn);
For this code to work you need to have the following
We have used ICollection class in our code ,so we have inherited System.Collections class files.One important thing to note is,when we open the AdminDAL class file for the first time you will have namespace as TESTDAL,replace that with TEST.DAL.
In this code I have connected the database using Connect() and closed the connection using closeconnection() .GetData() is used to fetch data from northwind database table Employees.
Now build the TESTDAL project as shown below
Now, Add this project reference to TESTBL project .Right click on the TESTBL project and choose option "AddReferance" as shown in figure.
You will get a browse option ,choose the project TESTDAL and go to Bin folder of that project ,you will get DEBUG folder.Inside this folder you will have TESTDAL.ddl as shown in figure
Choose that dll and press ok.
Now, open the class file Managegridview.cs and add this code in that file
public class Managegridview
public static ICollection GetData()
We have to inherit the following namespaces :
In the code we have used ICollection ,so we have used System.Collections and
inorder to access the functions in AdminDAL we have used Test.DAL namespace.
When we open the managegridview class file for the first time ,you will get
TESTBL .Replace that with TEST.BL.
Now again build this project as explained above and add this referance to the wesite TESTWEB .It is shown in figure below
In the Default.apx page of website add a grid view as shown below
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" Width="600px" HeaderStyle-BackColor="blue" RowStyle-BackColor="AliceBlue" CellPadding="4" ForeColor="#333333" GridLines="None">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
In the code view add this code
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
ICollection colcat = Managegridview.GetData();
if (colcat.Count > 0)
GridView1.DataSource = ((DataRowCollection)colcat).Table;
We have referanced TEST.BL to access functions in the project TESTBL and assigned Datasource to gridview1from GetData() in managegridview.cs file which in turn gets data from AdminDAL.cs file.when you run the website,you will have output as shown below
In this article I have mainly concentrated on how to create a 3 tier architecture website rather than technical part of it.You can use this article as a guideline to create your own website with 3 tier architecture containing functions which suit your needs.
A great and good informative article. Its knowledge worthy for freshera nd others too.
Keep it up.
Best of luck!