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. <head runat="server">
  3. <title></title>
  4. <style type="text/css">
  5. .auto-style1 {
  6. text-align: center;
  7. font-size: x-large;
  8. }
  9. .auto-style2 {
  10. width: 488px;
  11. }
  12. .auto-style3 {
  13. width: 95px;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <form id="form1" runat="server">
  19. <div>
  20. <table style="width:100%;">
  21. <tr>
  22. <td class="auto-style1" colspan="4"><strong>Employee Information</strong></td>
  23. </tr>
  24. <tr>
  25. <td class="auto-style2"> </td>
  26. <td class="auto-style3"> </td>
  27. <td> </td>
  28. <td> </td>
  29. </tr>
  30. <tr>
  31. <td class="auto-style2"> </td>
  32. <td class="auto-style3">Name :</td>
  33. <td>
  34. <asp:TextBox ID="txtName" runat="server" Width="200px"></asp:TextBox>
  35. </td>
  36. <td> </td>
  37. </tr>
  38. <tr>
  39. <td class="auto-style2"> </td>
  40. <td class="auto-style3">Address :</td>
  41. <td>
  42. <asp:TextBox ID="txtAddress" runat="server" Width="200px"></asp:TextBox>
  43. </td>
  44. <td> </td>
  45. </tr>
  46. <tr>
  47. <td class="auto-style2"> </td>
  48. <td class="auto-style3">Contact No :</td>
  49. <td>
  50. <asp:TextBox ID="txtContactNo" runat="server" Width="200px"></asp:TextBox>
  51. </td>
  52. <td> </td>
  53. </tr>
  54. <tr>
  55. <td class="auto-style2"> </td>
  56. <td class="auto-style3"> </td>
  57. <td>
  58. <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" Width="58px" />
  59. <asp:Label ID="lblMsg" runat="server"></asp:Label>
  60. </td>
  61. <td> </td>
  62. </tr>
  63. <tr>
  64. <td class="auto-style2"> </td>
  65. <td class="auto-style3"> </td>
  66. <td> </td>
  67. <td> </td>
  68. </tr>
  69. </table>
  70. </div>
  71. </form>
  72. </body>
  73. </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][int] NULL
  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:

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. }
  31. }