Three Tier Architecture In ASP.NET

Let's start creating a 3-Tier Architecture Application.

  1. Create a new project using "File", "New" , then "Project...".

    project

  2. In Visual C# select "Web".

    (Select "ASP.NET Web application" and name it as: ThreeTierApp).

  3. How to add class library to solution:

    solution

    After clicking on a new project you would see the following screen. Select "Class Library" from this and name it "BusinessObject".

    BusinessObject

    • Same(step: 3) as BusinessLogic, DataAccess.

    • ThreeTierApp(in Solution Explorer)- Reference, Add Reference, Solutions, then Projects click (BusinessObject,BusinessLogic, DataAccess) Click
     OK.

  4. BusinessObject, then EmployeeBO and add Class,

    Class

  5. BusinessObject, DBConnection add Class (For database Connection).

    Web.config:

    config

    config

    • Now we are done. Add all layers to our project.
    • You can see the three tiers in the image given below.

    solution

    Basically a 3-Tier architecture contains the following 3 layers:

    1. Application Layer or Presentation Layer (our web form and UI Part)
    2. Business Logic Layer (BusinessLogic)
    3. Data Access Layer (DataAccess)

  6. ThreeTierApp - add new webform,

    webform

Form Design

  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2.   
  3. <head runat="server">  
  4.     <title></title>  
  5.     <style type="text/css">  
  6.         .auto-style1 {  
  7.             text-align: center;  
  8.             font-size: x-large;  
  9.         }  
  10.           
  11.         .auto-style2 {  
  12.             width: 488px;  
  13.         }  
  14.           
  15.         .auto-style3 {  
  16.             width: 95px;  
  17.         }  
  18.     </style>  
  19. </head>  
  20.   
  21. <body>  
  22.     <form id="form1" runat="server">  
  23.         <div>  
  24.             <table style="width:100%;">  
  25.                 <tr>  
  26.                     <td class="auto-style1" colspan="4"><strong>Employee Information</strong></td>  
  27.                 </tr>  
  28.                 <tr>  
  29.                     <td class="auto-style2"> </td>  
  30.                     <td class="auto-style3"> </td>  
  31.                     <td> </td>  
  32.                     <td> </td>  
  33.                 </tr>  
  34.                 <tr>  
  35.                     <td class="auto-style2"> </td>  
  36.                     <td class="auto-style3">Name :</td>  
  37.                     <td>  
  38.                         <asp:TextBox ID="txtName" runat="server" Width="200px"></asp:TextBox>  
  39.                     </td>  
  40.                     <td> </td>  
  41.                 </tr>  
  42.                 <tr>  
  43.                     <td class="auto-style2"> </td>  
  44.                     <td class="auto-style3">Address :</td>  
  45.                     <td>  
  46.                         <asp:TextBox ID="txtAddress" runat="server" Width="200px"></asp:TextBox>  
  47.                     </td>  
  48.                     <td> </td>  
  49.                 </tr>  
  50.                 <tr>  
  51.                     <td class="auto-style2"> </td>  
  52.                     <td class="auto-style3">Contact No :</td>  
  53.                     <td>  
  54.                         <asp:TextBox ID="txtContactNo" runat="server" Width="200px"></asp:TextBox>  
  55.                     </td>  
  56.                     <td> </td>  
  57.                 </tr>  
  58.                 <tr>  
  59.                     <td class="auto-style2"> </td>  
  60.                     <td class="auto-style3"> </td>  
  61.                     <td>  
  62.                         <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" Width="58px" />  
  63.                         <asp:Label ID="lblMsg" runat="server"></asp:Label>  
  64.                     </td>  
  65.                     <td> </td>  
  66.                 </tr>  
  67.                 <tr>  
  68.                     <td class="auto-style2"> </td>  
  69.                     <td class="auto-style3"> </td>  
  70.                     <td> </td>  
  71.                     <td> </td>  
  72.                 </tr>  
  73.             </table>  
  74.         </div>  
  75.     </form>  
  76. </body>  
  77.   
  78. </html>  
Now let’s start to create a table for saving this data using our 3-Tier Architecture.
  1. CREATE TABLE[dbo].[Emp_Info]  
  2. (  
  3.     [Id][int] IDENTITY(1, 1) NOT NULL, [Name][varchar](50) NULL, [Address][varchar](200) NULL, [ContactNo][intNULL  
  4. ON[PRIMARY]  
Let's start with a business object first as in the following: Create a new class name, "EmployeeBO".
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace BusinessObject  
  7. {  
  8.     public class EmployeeBO  
  9.     {  
  10.         public string Name  
  11.         {  
  12.             get;  
  13.             set;  
  14.         }  
  15.         public string Address  
  16.         {  
  17.             get;  
  18.             set;  
  19.         }  
  20.         public int ContactNo  
  21.         {  
  22.             get;  
  23.             set;  
  24.         }  
  25.     }  
  26. }  
Now in the same way we created EmployeeBL in BusinessLogic, create a new class EmployeeDA in DataAccess.

The main thing TO DO

The main thing to do is to add the three layers:
  • EmployeeBO.cs
  • BusinessLogic.cs
  • EmployeeDA .cs

Last Step:

Save Button btnSave_Click

  1. using BusinessLogic; // Creating object of Business Layer  
  2. using BusinessObject; // Creating object of Business Object  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Web;  
  8. using System.Web.UI;  
  9. using System.Web.UI.WebControls;  
  10. namespace ThreeTierApp   
  11. {  
  12.     public partial class EmployeeInfo: System.Web.UI.Page   
  13.     {  
  14.         protected void Page_Load(object sender, EventArgs e) {}  
  15.         EmployeeBO oEmployeeBO = new EmployeeBO(); // Creating object of Business Object  
  16.         EmployeeBL oEmployeeBL = new EmployeeBL(); // Creating object of Business Logic  
  17.         protected void btnSave_Click(object sender, EventArgs e)   
  18.         {  
  19.             oEmployeeBO.Name = txtName.Text;  
  20.             oEmployeeBO.Address = txtAddress.Text;  
  21.             oEmployeeBO.ContactNo = Convert.ToInt32(txtContactNo.Text);  
  22.             string msg = oEmployeeBL.Save(oEmployeeBO);  
  23.             lblMsg.ForeColor = Color.Green;  
  24.             lblMsg.Text = msg.ToString();  
  25.         }  
  26.     }  
  27. }  
EmployeeBL.cs
  1. using BusinessObject;  
  2. using DataAccess;  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Linq;  
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8. namespace BusinessLogic  
  9. {  
  10.     public class EmployeeBL  
  11.     {  
  12.         EmployeeDA oEmployeeDA = new EmployeeDA(); // Creating object of Data Access  
  13.         public string Save(EmployeeBO oEmployeeBO)  
  14.         {  
  15.             return oEmployeeDA.Save(oEmployeeBO);  
  16.         }  
  17.     }  
  18. }  
EmployeeDA.cs
  1. using BusinessObject;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. namespace DataAccess  
  8. {  
  9.     public class EmployeeDA: DBConnection // Inherited DBConnection Class  
  10.         {  
  11.             public string Save(EmployeeBO oEmployeeBO)   
  12.             {  
  13.                 try  
  14.                 {  
  15.                     connection.Open();  
  16.                     string query = "insert into Emp_Info (Name,Address,ContactNo)values(@Name,@Address,@ContactNo)";  
  17.                     command.CommandText = query;  
  18.                     command.Parameters.Clear();  
  19.                     command.Parameters.AddWithValue("@Name", oEmployeeBO.Name);  
  20.                     command.Parameters.AddWithValue("@Address", oEmployeeBO.Address);  
  21.                     command.Parameters.AddWithValue("@ContactNo", oEmployeeBO.ContactNo);  
  22.                     command.ExecuteNonQuery();  
  23.                     connection.Close();  
  24.                     return "Save Success.";  
  25.                 } finally  
  26.                 {  
  27.                     connection.Close();  
  28.                 }  
  29.             }  
  30.         }