How to create a website using 3 tier architecture

Introduction:



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 :

Presentation Layer:



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.

figure2

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

dal

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

bl

Now, create a website with name TESTWEB in your visual studio as shown in figure

web

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

existing



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.

project1

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
3tier1

Now, in the AdminDAL of TESTDAL add this code


using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;



namespace TEST.DAL

{

public class AdminDAL

{

static SqlConnection conn;
static bool connected;

private static void Connect()

{

int count = 0;

try

{

conn = new SqlConnection("User ID=sa; Initial Catalog=Northwind;Integrated Security=SSPI;Persist Security Info=False; Data Source=localhost");



try

{

if (!connected)

{

conn.Open();

}



}

catch (Exception ex)

{

}

}

catch (SqlException ex)

{

}

return;

}

private static void closeconnection()

{

if (connected)

conn.Close();

}



public static ICollection GetData()

{

DataSet ds = new DataSet();

if (!connected)

Connect();



SqlDataAdapter da = new SqlDataAdapter("", conn);

string query = "SELECT Title,FirstName,LastName from Employees";

da.SelectCommand = new SqlCommand(query, conn);

da.Fill(ds, "Categories");

return ds.Tables["Categories"].Rows;

}



}



For this code to work you need to have the following


using System.Data.SqlClient;
using System.Data;
using System.Collections;



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

builddal

Now, Add this project reference to TESTBL project .Right click on the TESTBL project and choose option "AddReferance" as shown in figure.

referance2


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

refnext

Choose that dll and press ok.

Now, open the class file Managegridview.cs and add this code in that file


using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using TEST.DAL;


namespace TEST.BL

{

public class Managegridview

{

public static ICollection GetData()

{

return AdminDAL.GetData();

}

}

}



We have to inherit the following namespaces :


using System.Collections;
using TEST.DAL;


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

reflast1

In the Default.apx page of website add a grid view as shown below


<form id="form1" runat="server">

<div>

<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" />

</asp:GridView>



</div>

</form>


In the code view add this code


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using TEST.BL;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

ICollection colcat = Managegridview.GetData();

if (colcat.Count > 0)

{

GridView1.DataSource = ((DataRowCollection)colcat)[0].Table;

GridView1.DataBind();

}

}

}


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

output

Summary
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.


Attachments

  • 3tierarchitecture (25608-31253-3-TIER.zip)
  • Comments

    Author: Gaurav Aroraa06 Feb 2009 Member Level: Gold   Points : 1

    Hi,

    A great and good informative article. Its knowledge worthy for freshera nd others too.

    Keep it up.

    Best of luck!

    Author: PHANI HARSHITHA MADALA10 Feb 2009 Member Level: Gold   Points : 0

    hi ,

    Thanks for u r quick response.i really appreciate u r feedback.

    Author: Shuby Arora10 Feb 2009 Member Level: Gold   Points : 1

    Hi greeny,

    Really you have done a good job this is very good for freshera and for me also.

    keep it up


    keep smiling
    Shuby

    Author: ChandraShekar Thota11 Feb 2009 Member Level: Gold   Points : 1

    Excellent article. Concentrate on image clarity. This will be useful for beginners and your explaination is very good.
    Keep rocking.

    Author: PHANI HARSHITHA MADALA11 Feb 2009 Member Level: Gold   Points : 0

    Thanks for u r response

    Author: PHANI HARSHITHA MADALA11 Feb 2009 Member Level: Gold   Points : 0

    Thanks for u r response

    Author: fadi22 Feb 2009 Member Level: Bronze   Points : 0

    Thanks alot for this valuable information about N-Tier

    Author: TahiraSohaib18 Sep 2009 Member Level: Gold   Points : 0

    thnx greeny bt i need layering in asp.net with MS-Acess 2003 not with sql

    Author: Arun K18 Sep 2009 Member Level: Gold   Points : 1

    Great article.

    It has the basic idea about n-tier application

    and freshers or others can get idea frm it.

    Author: PHANI HARSHITHA MADALA18 Sep 2009 Member Level: Gold   Points : 1

    Hi,

    You can connect any database in the dataaccess layer as you normally connect.

    Author: vikas08 May 2010 Member Level: Bronze   Points : 0

    hi,
    good article. It is very good for freshers.

    Guest Author: joginder15 Feb 2012

    in .CS PAGE


    using System;
    using System.Data;
    using System.Configuration;

    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Data.SqlClient;


    namespace employee
    {

    public interface clsempprp
    {
    string p_eadd
    {
    get;
    set;


    }
    string p_esal
    {
    get;
    set;

    }
    string p_ename
    {

    get;
    set;

    }
    }


    public class clsemp : clsempprp
    {
    public string e_sal;
    public string e_add, e_name;


    #region clsempprp Members

    public string p_eadd
    {
    get
    {
    return e_add;
    }
    set
    {
    e_add = value;
    }
    }

    public string p_esal
    {
    get
    {
    return e_sal;
    }
    set
    {
    e_sal = value;
    }
    }

    public string p_ename
    {
    get
    {
    return e_name;
    }
    set
    {
    e_name = value;
    }
    }

    #endregion
    }
    public abstract class clscon
    {
    public SqlConnection con = new SqlConnection();
    public clscon()
    {
    con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString.ToString();
    }
    }
    public class clsempp : clscon
    {
    public void save(clsemp p)
    {
    if (con.State == ConnectionState.Closed)
    {
    con.Open();
    }

    SqlCommand cmd=new SqlCommand("inst_emp",con);
    cmd.CommandType=CommandType.StoredProcedure;
    cmd.Parameters.Add("@empname", SqlDbType.VarChar, 50).Value = p.e_name;
    cmd.Parameters.Add("@empadd", SqlDbType.VarChar, 50).Value = p.e_add;
    cmd.Parameters.Add("@empsal", SqlDbType.VarChar, 50).Value = p.e_sal;

    cmd.ExecuteNonQuery();
    cmd.Dispose();
    con.Close();
    }
    public void display(clsemp p)
    {

    con.Open();
    SqlDataAdapter adp =new SqlDataAdapter("select * from tier_table",con);
    DataSet ds = new DataSet();
    adp.Fill(ds);
    con.Close();

    }

    }
    }

    in .aspx
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;

    using System.Data.SqlClient;
    using employee;

    public partial class _Default : System.Web.UI.Page
    {

    clsemp c = new clsemp();
    clsempp d = new clsempp();

    protected void Page_Load(object sender, EventArgs e)
    {
    if(!IsPostBack)
    {

    ListBox1.DataSource=
    ListBox1.DataBind();

    }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    c.e_name = TextBox2.Text;

    c.e_sal = TextBox4.Text;
    c.e_add = TextBox3.Text;
    d.save(c);

    }


    Guest Author: Piyusha09 Apr 2012

    Thanks a lot this was the first example of 3 tier architecture which i could run without any problem...Thanks a lot ,very useful to beginers like me



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: