Skip to main content

3 Tier Architecture in ASP.NET

In this post i will show how to create 3 -tier architecture in asp.net.Before going to technical details,I will describe some details about what is 3 tier architecture and what are the main components of 3 tier architecture.

The three-tier architecture was comes into existence to improve management of code and contents and to improve the performance of the web based applications.There are mainly three layers in three-tier architecture.
These are define as follows…





1.  UI(InsertCustomer.aspx): First layer Presentation contains mainly the interface code, and this is shown to user. This code could contain any technology that can be used on the client side like HTML, JavaScript or VBScript etc.

2. Business Logic(CustomerBL.cs) : Second layer is Business Logic which contains all the code of the server-side .This layer have code to interact with database database and to query, manipulate, pass data to user interface and handle any input from the UI as well.

3. DataAccess Layer(CustomerDAL.cs) : Third layer Data represents the data store like MS Access, SQL Server, an XML file, an Excel file or even a text file containing data also some addtional database are also added to that layers.


Let's start creating the project step by step.
  • Create a new website in visual studio and add two folder inside App_Code named BL and DAL (In real scenario, you can create separate projects for BAL, DAL  and UI )



  • Now,Create a class named CustomerDAL into App_Code->DAL by right clicking it and write following code into it.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;


/// <summary>
/// Summary description for CustomerDAL
/// </summary>
public class CustomerDAL
{


    public  static void AddCustomer(CustomerBL cs)
    {
        try
        {
            using (SqlConnection con = new SqlConnection(SQLHelper.GetConnectionString()))
            {
                SqlParameter[] par = new SqlParameter[7];
                par[0] = new SqlParameter("@ID", cs.ID);
                par[0].Direction = ParameterDirection.Output;
                par[1] = new SqlParameter("@name", cs.Name);
                par[2] = new SqlParameter("@address", cs.Address);
                par[3] = new SqlParameter("@phoneNumber", cs.PhoneNumber);
                par[5] = new SqlParameter("@email", cs.Email);
                int rowNo = SQLHelper.ExecuteNonQuery(con, CommandType.StoredProcedure, "proc_InsertCustomer", par);
                cs.ID = Convert.ToInt32(par[0].Value);
            }
        }
        catch (Exception ex)
        {
            throw;
        }
    }
    public static void DeleteCustomer(CustomerBL customer)
    {
        try
        {
            using (SqlConnection con = new SqlConnection(SQLHelper.GetConnectionString()))
            {
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@ID", customer.ID);
                int rowNo = SQLHelper.ExecuteNonQuery(con, CommandType.StoredProcedure, "proc_DeleteCustomer", par);
            }
        }
        catch (Exception ex)
        {
            throw;
        }
    }
    public static void GetCustomer(CustomerBL customer)
    {
        try
        {
            using (SqlConnection con = new SqlConnection(SQLHelper.GetConnectionString()))
            {
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@ID", customer.ID);
                using (SqlDataReader dr = SQLHelper.ExecuteReader(con, CommandType.StoredProcedure, "OMS_Customer_SelectByCustomerID", par))
                {
                    while (dr.Read())
                    {
                        customer.Name = SQLHelper.CheckStringNull(dr["Name"]);
                        customer.PhoneNumber = SQLHelper.CheckStringNull(dr["PhoneNo"]);
                        customer.Address = SQLHelper.CheckStringNull(dr["Address"]);
                        customer.ID = SQLHelper.CheckIntNull(dr["ID"]);
                        customer.Email = SQLHelper.CheckStringNull(dr["Email"]);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw;
        }
    }

    public static List<CustomerBL> GetAllCustomers()
    {
        try
        {
            List<CustomerBL> cuList = new List<CustomerBL>();
            using (SqlConnection con = new SqlConnection(SQLHelper.GetConnectionString()))
            {
                using (SqlDataReader dr = SQLHelper.ExecuteReader(con, CommandType.StoredProcedure, "proc_SelectAllCustomer"))
                {
                    while (dr.Read())
                    {
                        CustomerBL customer = new CustomerBL();
                        customer.Name = SQLHelper.CheckStringNull(dr["Name"]);
                        customer.PhoneNumber = SQLHelper.CheckStringNull(dr["Phone"]);
                        customer.Address = SQLHelper.CheckStringNull(dr["Address"]);
                        customer.ID = SQLHelper.CheckIntNull(dr["ID"]);
                        cuList.Add(customer);
                    }
                }
            }
            return cuList;
        }
        catch (Exception ex)
        {
            throw;
        }
    }
}


  • Now,Create a class named CustomerBL into App_Code->BL by right clicking it and write following code into it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for CustomerBL
/// </summary>
public class CustomerBL
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string PhoneNumber { get; set; }
    public string Email { get; set; }
    public List<CustomerBL> Customers { get; set; }

    public void Add()
    {
        CustomerDAL.AddCustomer(this);
    }

    public void Delete()
    {
        CustomerDAL.DeleteCustomer(this);
    }


    public void GetAll()
    {
        this.Customers = CustomerDAL.GetAllCustomers();

    }
}


  • Now,Create a Page named InsertCustomer.aspx and add following code into it.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="InsertCustomer.aspx.cs" Inherits="InsertCustomer" %>

<!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 runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    <table class="style1">
        <tr>
            <td>
                Name
            </td>
            <td>
                <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Address
            </td>
            <td>
                <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Phone
            </td>
            <td>
                <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Email
            </td>
            <td>
                <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;
            </td>
            <td>
                <asp:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click" Text="Button" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>


and add follwoing code inside click event of button.



protected void btnInsert_Click(object sender, EventArgs e)
    {
        try
        {
            CustomerBL customer = new CustomerBL();
            customer.Name = txtName.Text;
            customer.PhoneNumber = txtAddress.Text;
            customer.Email = txtPhone.Text;
            customer.Address = txtEmail.Text;
            customer.Add();
        }
        catch (Exception)
        {
            
            throw;
        }



Download Code




Comments

  1. hi santosh ..good example for 3-tier structure
    pl send me SQLhelper.cs code to vinaykn.rao@gmail.com

    ReplyDelete
  2. Hello,

    Very good example since this procedure is followed in the industry.Please mail the Sqlhelper
    file to following the mail id pandurangav1611@gmail.com

    ReplyDelete
  3. can anyone send me code of SQLhelper.cs on schnjn968@gmail.com

    ReplyDelete
  4. Very good example for 3-tier structure. Pl send me code of SQLhelper.cs on vmcsekararun@gmail.com

    ReplyDelete
  5. plz send me the SQLhelper.cs file to arifrifa7@yahoo.com Thanx..*

    ReplyDelete
  6. This is good post
    please send me SQLhelper.cs code on this email:
    avadheshkumarmaurya@ymail.com

    Thanks...........

    ReplyDelete
  7. Very helpful can send me the SQLhelper.cs code on this email:
    Kulasekaralwar.c@gmail.com

    ReplyDelete
  8. Can u also explain me why we need to use global.asax file what is the purpose of it

    ReplyDelete
  9. can u give me dis project at
    kiransolkar@gmail.com

    ReplyDelete
  10. Can u pleaase give me the code for the following details.Write a program for employee in asp.net and the main page will open in any browser.When we open the browser it will ask uid and pwd and after that employee details.If anyone knows please send to this mail: dhina02kvp@gmail.com

    ReplyDelete
  11. Please send this project to swapnamandava950@gmail.com

    ReplyDelete
  12. Very helpful kindly send this project to rajeshk997@gmail.com

    ReplyDelete
  13. pls send me this project on kapil.it.sharma@gmail.com

    thnks .

    ReplyDelete
  14. Thanks Nice Post. Please give this post with coding on this Email Id: gaikwad.vj31@gmail.com

    ReplyDelete
  15. Hi, can I please get a copy of the source code on bronwinbergstedt@gmail.com. I cant get mine to work....

    thanks.

    ReplyDelete
  16. pls,... send this project at " tarun1089@gmail.com ".

    ReplyDelete
  17. What will write on sqlhelper class

    ReplyDelete
  18. please send the code of dbhelper class on my email id sonu.kumar.un@gmail.com

    ReplyDelete

Post a Comment

Popular posts from this blog