What is CrazyEngico.com (CE)?

CrazyEngico (CE) is a one-stop solution for interview questions and answers, domain knowledge, e-learning, tutorials, articles and many more learning material.
e-learning - Intelligent e-learning tutorials and articles
Domain Knowledge Learn about various business domains/verticals such as Banking, Financial, Insurance, Healthcare and many more...
Interview Questions and Answers - Post your questions and answers. 1000+ database of ready questions and answers available to prepare for an interview.
Tutorials - 300+ video tutorials from beginner to expert level.
Articles - 300+ technical articles to understand concepts.
Forum - A technical forum to post your queries/responses.

Search Articles
Search Query:


Ads
Create a website using three tier Architecture

Submitted by: Amey Pai Dhungat

Abstract: This article is about implementing a website using 3 – tier architecture using Visual Studio 2008.

Topics
  • Introduction
  • Implementation

Introduction

This architecture mainly consists of three layers –

1. Presentation Layer

This layer will mainly consist of user interface of the website.

2. Business Logic Layer

This layer acts as a mediator between Presentation layer and Data Access layer. This layer will have the business logic implementations and it transfers the data between the two layers.

3. Data Access Layer

This layer deals with the database and database related coding. We write stored procedures, queries in this layer.

4. Common layer

This is an optional layer.


Flow diagram


Implementation

Well, Lets get started!!!
1. Open MS Visual Studio 2008.
2. Create a project (file--new--project) with the name and choose option as “class library” as shown in picture. Give the file name as “tier.DAL”.

Create Project

Create Project

3. Repeat the second step for creating projects such as “tier.Common” and “tier.BLC”. DAL is used as Data access layer, BLC as Business layer, and common to hold common constants.
4.Save & close all the projects.
5. Open new MS VS 2008.
6. Now create new website by selecting file->new->Website and store it as given below.First create a new folder as tier.Website in the main projects folder, where you have all other projects.
7. Once that is done, add all projects under the solution(.sln) as shown below.


Create Project



Create Project

8. Once that is done, we got to save the main .sln file in folder “Project” where all the projects are stored.


Create Project



Create Project

9. Adding references : click on tier.BLC and add reference to tier.DAL. Similarly, for tier.web add reference to tier.BLC as follows –

Create Project

Once references are set, its time to edit the cs files.
NOTE: In general practice, .common project is used to store the common parameters, for ex. to pass between stored procedures and the DAL project. But for demonstration purpose , I have skipped Stored Procedures part.
10. Under tier.DAL, the file tierDAL.cs should contain –
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

namespace tier.DAL
{
    public class tierDAL
    {
        static SqlConnection conn;

        static bool connected;

        private static void Connect()

        {

            int count = 0;

            try

            {

                conn = new SqlConnection("User ID="Ur_userid"; Initial Catalog="ur_Initial_catalog"; 
                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;

        }

    }
}

    

12. In the Default.apx page, add a gridview control. Refer the below aspx code,



Create Project

13. In the code behind, we need to call the Business logic layer’s GetData() method to bind the GridView. Refer the below code,
using System;
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.Xml.Linq;

using System.Collections;
using tier.BLC;
using tier.DAL;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            ICollection colcat = tierBLC.GetData();

            if (colcat.Count > 0)
            {

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

                GridView1.DataBind();

            }
        }

    }
}

    

That’s it. Build the solution now, run your project and see the output in browser. You will get output somewhat similiar to this –


Create Project


Copyright © 2017 CrazyEngico.com. All rights reserved.
CrazyEngico.com is not responsible for the content and CrazyEngico.com does not evaluate or guarantee the accuracy of any CrazyEngico.com content.
Please read our terms of service agreement before using this site.

feedback