Let's start creating a 3-Tier Architecture Application.
- Create a new project using "File", "New" , then "Project...".
- In Visual C# select "Web".
(Select "ASP.NET Web application" and name it as: ThreeTierApp).
- How to add class library to solution:
After clicking on a new project you would see the following screen. Select "Class Library" from this and name it "BusinessObject".
• Same(step: 3) as BusinessLogic, DataAccess.
• ThreeTierApp(in Solution Explorer)- Reference, Add Reference, Solutions, then Projects click (BusinessObject,BusinessLogic, DataAccess) Click
OK.
- BusinessObject, then EmployeeBO and add Class,
- BusinessObject, DBConnection add Class (For database Connection).
Web.config:
• Now we are done. Add all layers to our project.
• You can see the three tiers in the image given below.
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)
- ThreeTierApp - add new webform,
Form Design
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head runat="server">
- <title></title>
- <style type="text/css">
- .auto-style1 {
- text-align: center;
- font-size: x-large;
- }
-
- .auto-style2 {
- width: 488px;
- }
-
- .auto-style3 {
- width: 95px;
- }
- </style>
- </head>
-
- <body>
- <form id="form1" runat="server">
- <div>
- <table style="width:100%;">
- <tr>
- <td class="auto-style1" colspan="4"><strong>Employee Information</strong></td>
- </tr>
- <tr>
- <td class="auto-style2"> </td>
- <td class="auto-style3"> </td>
- <td> </td>
- <td> </td>
- </tr>
- <tr>
- <td class="auto-style2"> </td>
- <td class="auto-style3">Name :</td>
- <td>
- <asp:TextBox ID="txtName" runat="server" Width="200px"></asp:TextBox>
- </td>
- <td> </td>
- </tr>
- <tr>
- <td class="auto-style2"> </td>
- <td class="auto-style3">Address :</td>
- <td>
- <asp:TextBox ID="txtAddress" runat="server" Width="200px"></asp:TextBox>
- </td>
- <td> </td>
- </tr>
- <tr>
- <td class="auto-style2"> </td>
- <td class="auto-style3">Contact No :</td>
- <td>
- <asp:TextBox ID="txtContactNo" runat="server" Width="200px"></asp:TextBox>
- </td>
- <td> </td>
- </tr>
- <tr>
- <td class="auto-style2"> </td>
- <td class="auto-style3"> </td>
- <td>
- <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" Width="58px" />
- <asp:Label ID="lblMsg" runat="server"></asp:Label>
- </td>
- <td> </td>
- </tr>
- <tr>
- <td class="auto-style2"> </td>
- <td class="auto-style3"> </td>
- <td> </td>
- <td> </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
-
- </html>
Now let’s start to create a table for saving this data using our 3-Tier Architecture.
- CREATE TABLE[dbo].[Emp_Info]
- (
- [Id][int] IDENTITY(1, 1) NOT NULL, [Name][varchar](50) NULL, [Address][varchar](200) NULL, [ContactNo][int] NULL
- ) ON[PRIMARY]
Let's start with a business object first as in the following: Create a new class name, "
EmployeeBO".
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace BusinessObject
- {
- public class EmployeeBO
- {
- public string Name
- {
- get;
- set;
- }
- public string Address
- {
- get;
- set;
- }
- public int ContactNo
- {
- get;
- set;
- }
- }
- }
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
- using BusinessLogic;
- using BusinessObject;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace ThreeTierApp
- {
- public partial class EmployeeInfo: System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e) {}
- EmployeeBO oEmployeeBO = new EmployeeBO();
- EmployeeBL oEmployeeBL = new EmployeeBL();
- protected void btnSave_Click(object sender, EventArgs e)
- {
- oEmployeeBO.Name = txtName.Text;
- oEmployeeBO.Address = txtAddress.Text;
- oEmployeeBO.ContactNo = Convert.ToInt32(txtContactNo.Text);
- string msg = oEmployeeBL.Save(oEmployeeBO);
- lblMsg.ForeColor = Color.Green;
- lblMsg.Text = msg.ToString();
- }
- }
- }
EmployeeBL.cs - using BusinessObject;
- using DataAccess;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace BusinessLogic
- {
- public class EmployeeBL
- {
- EmployeeDA oEmployeeDA = new EmployeeDA();
- public string Save(EmployeeBO oEmployeeBO)
- {
- return oEmployeeDA.Save(oEmployeeBO);
- }
- }
- }
EmployeeDA.cs
- using BusinessObject;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace DataAccess
- {
- public class EmployeeDA: DBConnection
- {
- public string Save(EmployeeBO oEmployeeBO)
- {
- try
- {
- connection.Open();
- string query = "insert into Emp_Info (Name,Address,ContactNo)values(@Name,@Address,@ContactNo)";
- command.CommandText = query;
- command.Parameters.Clear();
- command.Parameters.AddWithValue("@Name", oEmployeeBO.Name);
- command.Parameters.AddWithValue("@Address", oEmployeeBO.Address);
- command.Parameters.AddWithValue("@ContactNo", oEmployeeBO.ContactNo);
- command.ExecuteNonQuery();
- connection.Close();
- return "Save Success.";
- } finally
- {
- connection.Close();
- }
- }
- }
- }