This article contains 4-tier architecture with SQL ServerCE (Compact Edition) database. It includes analyzing, designing and coding for a Employee detail record present in the database by using 4-tier architecture.
INTRODUCTION:
This
article gives you the brief idea on 4-tier architecture. When you are doing a
simple application, you are writing all codes in a single program. So that it
will make complexity in your program. To overcome this you are using 3-tier architecture.
In this you are separating your application into 3 different layers. In this
architecture also you are declaring the objects and their access properties in
a single layer and again it will create a bit of complexity. To overcome this
bit of complexity I have implemented 4-tier architecture. In this I have
divided the whole application into 4 different layers. Here I have created a
separate layer for actual objects and no need to write long function parameters
in a single layer like in 3-tier architecture. With this you can access data
easily, manipulation, easy to find errors, and also you can change the object
definition without accessing the Business access layer.
Now
let’s explain you step by step process to create 4-tier architecture.
In
this application I am going to take an Employee detail that will have EmpID,
Name, Age, Salary, Gender, DOB (Date of Birth), Address, ContactNo, EmailID.
Here I will perform Insert, Update and Delete operation to the data.
In
this application we will have the following 4 layers.
- 1)
Business Objects (BO)
- 2)
Data Access Layer (DAL)
- 3)
Business Access Layer (BAL)
- 4)
User Interface (UI)
Now
to carry on this process you have to follow the following steps.
STEP-1:
Create
a new web site with a name “EmployeeDetail.web”.

Then
give the website name as mentioned above and choose the location. Then remove
the default pages and Account folder.

STEP-2:
Create
a table in the database with above mentioned contents. For creation of table I
have used SQL ServerCE (Compact Edition) database.
Steps to create a table in
SQL Server CE database:
·
First you have to install SQL Server CE
software.
·
After that go to server explorer in left
panel of your created application, and right click on Database connections,
click on Add Connection…

·
Give database name as EmployeeData and password as abc,
click on create, then OK.

·
To create a table in that created database
again go to Server Explorer, expand EmployeeData.sdf.
Then right click on Tables menu and click on create table.

·
Now give the table name as EmployeeDetail and initialize the column
names and their types. Then click ok.

·
Now your table is created inside the
database.
·
To use the SQL Server CE you have write the
below mentioned connection string in the web.config file.
<connectionStrings>
<add name="SqlConn" connectionString="@Data Source=E:\Balaji\EmpFourTier\Bin\EmployeeData.sdf;password=abc;Persist Security Info=False"providerName="System.Data.SqlClient"/>
</connectionStrings>
·
And also mention the below code inside the
Global.asax
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true);
}
Now let’s create the above
mentioned tiers one by one.
STEP-3:
BUSINESS OBJECTS (BO):
Create a new project with
a name “EmployeeDetail.BO”
To create this, follow the
steps:
>>Open Visual Studio
>>click file menu >>click new >>select project >>choose
class library template >>Give the name as “EmployeeDetail.BO”
>>Then click ok.

After creating, rename the
class name from class1.cs to EmployeeDetailBO.cs
Code
with in EmployeeDetailBO.cs :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
/// <summary>
/// Properties for each field in EmployeeDetailBO
/// </summary>
namespace EmployeeDetail.BO
{
public class EmployeeDetailBO
{
public int EmpID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public int Salary { get; set; }
public string Gender { get; set; }
public DateTime DOB { get; set; }
public string Address { get; set; }
public string ContactNo { get; set; }
public string EmailID { get; set; }
}
}
In the same way create another two
class library file for DAL and BAL
STEP-4:
DATA ACCESS LAYER (DAL):
Create a new project with
a name “EmployeeDetail.DAL”
In this application I want
to perform Insert, Update, Delete and load operation to the database. So in
this layer declare all that properties.
Here also rename the
class1.cs as EmployeeDetailDAL.cs
Code
with in EmployeeDetailDAL.cs :
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlServerCe;
using EmployeeDetail.BO;
namespace EmployeeDetail.DAL
{
/// <summary>
/// Summary description for EmployeeDetailDAL
/// </summary>
public class EmployeeDetailDAL
{
string connstr = ConfigurationManager.ConnectionStrings["SqlConn"].ToString();
public EmployeeDetailDAL()
{
}
/// <summary>
/// Insert Records into Database
/// </summary>
/// <param name="EmpDetailBO"></param>
/// <returns></returns>
public int Insert(EmployeeDetailBO EmpDetailBO)
{
SqlCeConnection conn = new SqlCeConnection(connstr);
conn.Open();
SqlCeCommand cmd = new SqlCeCommand("Insert into EmployeeDetail (EmpID, Name, Age, Salary, Gender, DOB, Address, ContactNo, EmailID) values (@EmpID, @Name, @Age, @Salary, @Gender, @DOB, @Address, @ContactNo, @EmailID)", conn);
try
{
cmd.Parameters.AddWithValue("@EmpID", EmpDetailBO.EmpID);
cmd.Parameters.AddWithValue("@Name", EmpDetailBO.Name);
cmd.Parameters.AddWithValue("@Age", EmpDetailBO.Age);
cmd.Parameters.AddWithValue("@Salary", EmpDetailBO.Salary);
cmd.Parameters.AddWithValue("@Gender", EmpDetailBO.Gender);
cmd.Parameters.AddWithValue("@DOB", EmpDetailBO.DOB);
cmd.Parameters.AddWithValue("@Address", EmpDetailBO.Address);
cmd.Parameters.AddWithValue("@ContactNo", EmpDetailBO.ContactNo);
cmd.Parameters.AddWithValue("@EmailID", EmpDetailBO.EmailID);
return cmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
conn.Close();
cmd.Dispose();
conn.Dispose();
}
}
/// <summary>
/// Update Records in the Database
/// </summary>
/// <param name="EmpDetailBO"></param>
/// <returns></returns>
public int Update(EmployeeDetailBO EmpDetailBO)
{
SqlCeConnection conn = new SqlCeConnection(connstr);
conn.Open();
SqlCeCommand cmd = new SqlCeCommand("Update EmployeeDetail set Name=@Name, Age=@Age, Salary=@Salary, Gender=@Gender, DOB=@DOB, Address=@Address, ContactNo=@ContactNo, EmailID=@EmailID where EmpID=@EmpID ", conn);
try
{
cmd.Parameters.AddWithValue("@EmpID", EmpDetailBO.EmpID);
cmd.Parameters.AddWithValue("@Name", EmpDetailBO.Name);
cmd.Parameters.AddWithValue("@Age", EmpDetailBO.Age);
cmd.Parameters.AddWithValue("@Salary", EmpDetailBO.Salary);
cmd.Parameters.AddWithValue("@Gender", EmpDetailBO.Gender);
cmd.Parameters.AddWithValue("@DOB", EmpDetailBO.DOB);
cmd.Parameters.AddWithValue("@Address", EmpDetailBO.Address);
cmd.Parameters.AddWithValue("@ContactNo", EmpDetailBO.ContactNo);
cmd.Parameters.AddWithValue("@EmailID", EmpDetailBO.EmailID);
return cmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
conn.Close();
cmd.Dispose();
conn.Dispose();
}
}
/// <summary>
/// Delete Records from Database
/// </summary>
/// <param name="EmpDetailBO"></param>
/// <returns></returns>
public int Delete(EmployeeDetailBO EmpDetailBO)
{
SqlCeConnection conn = new SqlCeConnection(connstr);
conn.Open();
SqlCeCommand cmd = new SqlCeCommand("Delete EmployeeDetail where EmpID=@EmpID", conn);
try
{
cmd.Parameters.AddWithValue("@EmpID", EmpDetailBO.EmpID);
return cmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
conn.Close();
cmd.Dispose();
conn.Dispose();
}
}
/// <summary>
/// Load Records from Database
/// </summary>
/// <returns></returns>
public DataTable Load()
{
SqlCeConnection conn = new SqlCeConnection(connstr);
conn.Open();
SqlCeDataAdapter da = new SqlCeDataAdapter("select * from EmployeeDetail", conn);
DataSet ds = new DataSet();
try
{
da.Fill(ds, "EmployeeDetail");
return ds.Tables["EmployeeDetail"];
}
catch
{
throw;
}
finally
{
conn.Close();
ds.Dispose();
da.Dispose();
}
}
}
}
STEP-5:
BUSINESS ACCESS LAYER (BAL):
Again create a new
project with a name “EmployeeDetail.BAL”
Here also rename the
class1.cs as EmployeeDetailBAL.cs
Code
with in EmployeeDetailBAL.cs:
using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using EmployeeDetail.BO;
using EmployeeDetail.DAL;
namespace EmployeeDetail.BAL
{
/// <summary>
/// Summary description for EmployeeDetailBAL
/// </summary>
public class EmployeeDetailBAL
{
public EmployeeDetailBAL()
{
}
/// <summary>
/// Insert Records into Database
/// </summary>
/// <param name="EmpDetailBO"></param>
/// <returns></returns>
public int Insert(EmployeeDetailBO EmpDetailBO)
{
EmployeeDetailDAL EmpDetailDAL = new EmployeeDetailDAL();
try
{
return EmpDetailDAL.Insert(EmpDetailBO);
}
catch
{
throw;
}
finally
{
EmpDetailDAL = null;
}
}
/// <summary>
/// Update Records in the Database
/// </summary>
/// <param name="EmpDetailBO"></param>
/// <returns></returns>
public int Update(EmployeeDetailBO EmpDetailBO)
{
EmployeeDetailDAL EmpDetailDAL = new EmployeeDetailDAL();
try
{
return EmpDetailDAL.Update(EmpDetailBO);
}
catch
{
throw;
}
finally
{
EmpDetailDAL = null;
}
}
/// <summary>
/// Delete Records from Database
/// </summary>
/// <param name="EmpDetailBO"></param>
/// <returns></returns>
public int Delete(EmployeeDetailBO EmpDetailBO)
{
EmployeeDetailDAL EmpDetailDAL = new EmployeeDetailDAL();
try
{
return EmpDetailDAL.Delete(EmpDetailBO);
}
catch
{
throw;
}
finally
{
EmpDetailDAL = null;
}
}
/// <summary>
/// Load Records from the Database
/// </summary>
/// <returns></returns>
public DataTable Load()
{
EmployeeDetailDAL EmpDetailDAL = new EmployeeDetailDAL();
try
{
return EmpDetailDAL.Load();
}
catch
{
throw;
}
finally
{
EmpDetailDAL = null;
}
}
}
}
Here I have created
separate methods for unique methods that are present in data access layer.
Up to now we have created
BO, DAL, and BAL. So now the task is to create UI layer.
STEP-6:
USER
INTERFACE (UI):
For this I have created
three different web sites that are Default.aspx, EmployeeDetailData.aspx and
EmployeeDetailInsert.aspx. Now I will explain you one by one so that how you
will design each page.
For
Default.aspx:
In this page I have putted
a head tag to show “Employee Detail Record” when you will run Default.aspx.
Also I have given links to the other two pages.
Mentioned the below code
in the source code of Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<h1>
Employee Detail Record
</h1>
<div>
<a href="EmployeeDetailData.aspx">EmployeeDetail Record</a>
<br /> <br />
<a href="EmployeeDetailInsert.aspx">Insert to EmployeeDetail</a>
</div>
</form>
</body>
</html>
Here
no need to write any code in code behind page.
When
you will run this page you will get like below given picture. Here you can
click on the mentioned link and you can easily redirect to that page.
For
EmployeeDetailData.aspx:
Here I have declared a
head tag, hyperlink to other pages. To view the records present in database I
have used a Grid View control.
Mentioned the below code
in the source page of EmployeeDetailData.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EmployeeDetailData.aspx.cs"Inherits="EmployeeDetailData" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<h1>
Employee Detail Record
</h1>
<div>
<a href="EmployeeDetailData.aspx">EmployeeDetail Record</a>
<br /> <br />
<a href="EmployeeDetailInsert.aspx">Insert to EmployeeDetail</a>
<br /><br />
<asp:GridView ID ="GridView1" runat ="server" AllowPaging ="true"
AllowSorting ="true" PageSize ="3" AutoGenerateColumns ="false"
OnRowDeleting ="DeleteRecord" DataKeyNames ="EmpID"
OnPageIndexChanging="ChangePage" AutoGenerateEditButton ="true"
OnRowEditing ="EditRecord" OnRowCancelingEdit ="CancelRecord"
OnRowUpdating ="UpdateRecord" BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black"
GridLines="None">
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<Columns>
<asp:BoundField DataField ="EmpID" HeaderText ="Emp ID" ReadOnly ="true" />
<asp:TemplateField HeaderText ="Name" SortExpression ="Name">
<ItemTemplate >
<%# Eval ("Name") %>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID ="txtName" runat ="server" Text ='<%# Eval ("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Age" SortExpression ="Age">
<ItemTemplate >
<%# Eval ("Age") %>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID ="txtAge" runat ="server" Text ='<%# Eval ("Age") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Salary" SortExpression ="Salary">
<ItemTemplate >
<%# Eval ("Salary") %>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID ="txtSal" runat ="server" Text ='<%# Eval ("Salary") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Gender" SortExpression ="Gender">
<ItemTemplate >
<%# Eval ("Gender") %>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID ="txtGen" runat ="server" Text ='<%# Eval ("Gender") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="DOB" SortExpression ="DOB">
<ItemTemplate >
<%# Eval ("DOB") %>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID ="txtDob" runat ="server" Text ='<%# Eval ("DOB") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Address" SortExpression ="Address">
<ItemTemplate >
<%# Eval ("Address") %>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID ="txtAdd" runat ="server" TextMode ="MultiLine" Rows ="3" Columns ="25"Text ='<%# Eval ("Address") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="ContactNo" SortExpression ="ContactNo">
<ItemTemplate >
<%# Eval ("ContactNo") %>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID ="txtCon" runat ="server" Text ='<%# Eval ("ContactNo") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="EmailID" SortExpression ="EmailID">
<ItemTemplate >
<%# Eval ("EmailID") %>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID ="txtEid" runat ="server" Text ='<%# Eval ("EmailID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete" HeaderStyle-ForeColor ="Red" >
<ItemTemplate>
<span onclick="return confirm('Are you sure to Delete?')">
<asp:LinkButton ID="lnBD" runat="server" Text="Delete" CommandName="Delete"ForeColor ="Red" ></asp:LinkButton>
</span>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Code
for Code behind Page:
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlServerCe;
using EmployeeDetail.BO;
using EmployeeDetail.BAL;
public partial class EmployeeDetailData : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
GridBind();
}
private DataTable GridDataSource()
{
EmployeeDetailBAL EmpDetailBAL = new EmployeeDetailBAL();
DataTable dTable = new DataTable();
try
{
dTable = EmpDetailBAL.Load();
}
catch (Exception ee)
{
ee.Message.ToString();
}
finally
{
EmpDetailBAL = null;
}
return dTable;
}
private void GridBind()
{
GridView1.DataSource = GridDataSource();
GridView1.DataBind();
}
protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
{
int EmpID = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
EmployeeDetailBAL EmpDetailBAL = new EmployeeDetailBAL();
EmployeeDetailBO EmpDetailBO = new EmployeeDetailBO();
try
{
EmpDetailBO.EmpID = EmpID;
EmpDetailBAL.Delete(EmpDetailBO);
Response.Write("Record Deleted Successfully");
}
catch (Exception ee)
{
ee.Message.ToString();
}
finally
{
EmpDetailBO = null;
EmpDetailBAL = null;
}
GridView1.EditIndex = -1;
GridBind();
}
protected void CancelRecord(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridBind();
}
protected void EditRecord(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridBind();
}
protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
{
int EmpID = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
int intResult = 0;
GridViewRow row = GridView1.Rows[e.RowIndex];
TextBox tName = (TextBox)row.FindControl("txtName");
TextBox tAge = (TextBox)row.FindControl("txtAge");
TextBox tSal = (TextBox)row.FindControl("txtSal");
TextBox tGen = (TextBox)row.FindControl("txtGen");
TextBox tDOB = (TextBox)row.FindControl("txtDob");
TextBox tAdd = (TextBox)row.FindControl("txtAdd");
TextBox tCno = (TextBox)row.FindControl("txtCon");
TextBox tEmailid = (TextBox)row.FindControl("txtEid");
EmployeeDetailBAL EmpDetailBAL = new EmployeeDetailBAL();
EmployeeDetailBO EmpDetailBO = new EmployeeDetailBO();
try
{
EmpDetailBO.EmpID = EmpID;
EmpDetailBO.Name = tName.Text;
EmpDetailBO.Age = int.Parse(tAge.Text);
EmpDetailBO.Salary = int.Parse(tSal.Text);
EmpDetailBO.Gender = tGen.Text;
EmpDetailBO.DOB = DateTime.Parse(tDOB.Text);
EmpDetailBO.Address = tAdd.Text;
EmpDetailBO.ContactNo = tCno.Text;
EmpDetailBO.EmailID = tEmailid.Text;
intResult = EmpDetailBAL.Update(EmpDetailBO);
if (intResult > 0)
Response.Write("Record Updated Successfully");
else
Response.Write("Record couldn't Update");
}
catch (Exception ee)
{
ee.Message.ToString();
}
finally
{
EmpDetailBO = null;
EmpDetailBAL = null;
}
GridView1.EditIndex = -1;
GridBind();
}
protected void ChangePage(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridBind();
}
}
With
the help of this you can perform Update, Delete, Load operation to the data
present in the database.
When you will run this
page, you will get like below given picture.

When you will click on
edit link, a below mentioned picture will show.

In this window, you can
edit the existing data. When you will click on Update link it will update the
record into the database.
For
EmployeeDetailInsert.aspx
Mention the below code in
the source code as
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EmployeeDetailInsert.aspx.cs"Inherits="EmployeeDetailInsert" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<h1>
Employee Detail Record
</h1>
<div>
<a href="EmployeeDetailData.aspx">EmployeeDetail Record</a>
<br /> <br />
<a href="EmployeeDetailInsert.aspx">Insert to EmployeeDetail</a>
<br /><br />
<table style ="border :2px solid #cccccc;">
<tr >
<td>
EmpID:
</td>
<td>
<asp:TextBox ID ="txtEmpID" runat ="server" ></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID ="req1" runat ="server" Text ="*" ControlToValidate ="txtEmpID"Display ="Dynamic" ></asp:RequiredFieldValidator>
<asp:CompareValidator ID ="com1" runat ="server" Text ="Only integer" ControlToValidate="txtEmpID" Operator ="DataTypeCheck" Type ="Integer" ></asp:CompareValidator>
</td>
</tr>
<tr>
<td>
Name:
</td>
<td>
<asp:TextBox ID ="txtName" runat ="server" ></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID ="req2" runat ="server" Text ="*" ControlToValidate ="txtName"Display ="Dynamic" ></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Age:
</td>
<td>
<asp:TextBox ID ="txtAge" runat ="server" ></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID ="req6" runat ="server" Text ="*" ControlToValidate ="txtAge"Display ="Dynamic"></asp:RequiredFieldValidator>
<asp:CompareValidator ID ="com3" runat ="server" Text ="Only integer" ControlToValidate="txtAge" Operator ="DataTypeCheck" Type ="Integer" ></asp:CompareValidator>
</td>
</tr>
<tr>
<td>
Salary:
</td>
<td>
<asp:TextBox ID ="txtSalary" runat ="server" ></asp:TextBox>
</td>
<td >
<asp:RequiredFieldValidator ID ="req7" runat ="server" Text ="*" ControlToValidate="txtSalary" Display ="Dynamic" ></asp:RequiredFieldValidator>
<asp:CompareValidator ID ="com4" runat ="server" Text ="Only integer" ControlToValidate="txtSalary" Operator ="DataTypeCheck" Type ="Integer" ></asp:CompareValidator>
</td>
</tr>
<tr>
<td>
Gender:
</td>
<td>
<asp:DropDownList ID ="ddlGender" runat ="server" >
<asp:ListItem Text ="Male" Value ="Male"></asp:ListItem>
<asp:ListItem Text ="Female" Value ="Female"></asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:RequiredFieldValidator ID ="req4" runat ="server" Text ="*" ControlToValidate="ddlGender" Display ="Dynamic" ></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
DOB:
</td>
<td>
<asp:TextBox ID ="txtDOB" runat="server" ></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID ="req5" runat ="server" Text ="*" ControlToValidate ="txtDOB"Display ="Dynamic" ></asp:RequiredFieldValidator>
<asp:CompareValidator ID ="com2" runat ="server" Text ="Date Type" ControlToValidate="txtDOB" Operator ="DataTypeCheck" Type ="Date" ></asp:CompareValidator>
</td>
</tr>
<tr>
<td valign ="top">
Address:
</td>
<td>
<asp:TextBox ID ="txtAddress" runat ="server" TextMode ="MultiLine" Rows ="3" Columns ="25" ></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID ="req8" runat ="server" Text ="*" ControlToValidate="txtAddress" Display ="Dynamic" ></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
ContactNo:
</td>
<td>
<asp:TextBox ID ="txtContactNo" runat ="server" ></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID ="req9" runat ="server" Text ="*" ControlToValidate="txtContactNo" Display ="Dynamic" ></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Email-ID:
</td>
<td>
<asp:TextBox ID ="txtEmailID" runat ="server" ></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID ="req10" runat ="server" Text ="*" ControlToValidate="txtEmailID" Display ="Dynamic" ></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align ="center">
<asp:Button ID ="btnsubmit" runat ="server" Text ="Submit" OnClick ="AddRecords"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Code
for code behind page:
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EmployeeDetail.BO;
using EmployeeDetail.BAL;
public partial class EmployeeDetailInsert : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void AddRecords(object sender, EventArgs e)
{
if (!IsPostBack)
return;
int intResult = 0;
EmployeeDetailBAL EmpDetailBAL = new EmployeeDetailBAL();
EmployeeDetailBO EmpDetailBO = new EmployeeDetailBO();
EmpDetailBO.EmpID = int.Parse(txtEmpID.Text);
EmpDetailBO.Name = txtName.Text;
EmpDetailBO.Age = int.Parse(txtAge.Text);
EmpDetailBO.Salary = int.Parse(txtSalary.Text);
EmpDetailBO.Gender = ddlGender.SelectedValue;
EmpDetailBO.DOB = DateTime.Parse(txtDOB.Text);
EmpDetailBO.Address = txtAddress.Text;
EmpDetailBO.ContactNo = txtContactNo.Text;
EmpDetailBO.EmailID = txtEmailID.Text;
try
{
intResult = EmpDetailBAL.Insert(EmpDetailBO);
if (intResult > 0)
Response.Write("Record Inserted Successfully");
else
Response.Write("Record already exist, try another");
}
catch (Exception ee)
{
ee.Message.ToString();
}
finally
{
EmpDetailBO = null;
EmpDetailBAL = null;
}
Response.Redirect("EmployeeDetailData.aspx");
}
}
When you will run this .aspx page, you will get a form with blank
textboxes. There you have to insert the records. By running this, you will get
below mentioned picture.

After submitting all the required data into the textboxes, when you
will click on submit button it will insert the records into the database and
will redirect to the “EmployeeDetailData.aspx” page as mentioned below.

Here by clicking on 1, 2 page indexes you can navigate from one webpage to another page in this application.
Now we have completed all of our tasks. Now just you built the
application by pressing Ctrl+Shft+B. When it will show ‘Built Successed’ in the
status bar, after that you will get the benefit of using 4-tier architecture.
Try to manipulate the data present in database and you will feel how effective
and user friendly it is. With the help of this simple example you can also
build a large application by adding different class files, object files and
their corresponding tiers into the website.
You can also directly copy pasting the above mentioned code as I have
mentioned, and you will get the benefit of using 4-tier architecture.
Hope you will get all the benefits what I have mentioned above. So
let’s enjoy 4-tier architecture.