Download
Download source code for 4-Tier Architecture in ASP.NET with C#
Well, the architecture I am going to demonstrate here is just enhancement of 3-Tier archicture. In this architecture; you no need of writing long function parameters throughout the layers (as in traditionally 3-Tier archicture has to) and the actual objects of the application will be in a separate tier so that in future you can separately use these objects for enhancements. Change in the object definition can be done without touching the entire Business Access Layers ............
Let me explain you step-wise process of creatioin of 4-Tier architecture application.
In this application, I am going to take example of a Person that will have 3 properties: FirstName, LastName, Age. We will create a separate pages to insert these records (default.aspx) into database and list,update,delete records (list.aspx) from database.
design a table in ur database the table name is EMP
create a procedure on ur database the procedure like this
CREATE PROCEDURE EMP1
(
@EID INT=NULL,
@NAME VARCHAR(50)=NULL,
@MARK INT
)
AS
BEGIN
SET NOCOUNT ON
IF @MARK=1
INSERT INTO EMP VALUES(@EID,@NAME)
ELSE IF @MARK=2
UPDATE EMP SET NAME=@NAME WHERE EID=@EID
ELSE IF @MARK=3
DELETE FROM EMP WHERE EID=@EID
ELSE IF @MARK=4
SELECT * FROM EMP
SET NOCOUNT OFF
END
(
@EID INT=NULL,
@NAME VARCHAR(50)=NULL,
@MARK INT
)
AS
BEGIN
SET NOCOUNT ON
IF @MARK=1
INSERT INTO EMP VALUES(@EID,@NAME)
ELSE IF @MARK=2
UPDATE EMP SET NAME=@NAME WHERE EID=@EID
ELSE IF @MARK=3
DELETE FROM EMP WHERE EID=@EID
ELSE IF @MARK=4
SELECT * FROM EMP
SET NOCOUNT OFF
END
In this application we will have following 4-Tiers
1. Business Object [BO]
2. Business Access Layer [BAL]
3. Data Access Layer [DAL]
4. User Interface. [UI]
Add a class on ur project, the class name is BO.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class BO
{
private int eid;
private string name;
public int EID
{
get
{
return eid;
}
set
{
eid = value;
}
}
public string NAME
{
set
{
name = value;
}
get
{
return name;
}
}
}
Add another class on ur project, the class name is DAL.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public class DAL
{
SqlCommand cm;
SqlConnection cn;
SqlDataAdapter da;
DataTable dt = new DataTable();
public DAL()
{
cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ToString());
cn.Open();
}
public int insert(BO o)
{
try
{
cm = new SqlCommand("emp1", cn);
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.AddWithValue("@eid", o.EID);
cm.Parameters.AddWithValue("@name", o.NAME);
cm.Parameters.AddWithValue("@mark", 1);
return cm.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
cm.Dispose();
cn.Close();
cn.Dispose();
}
}
public int update(BO o)
{
try
{
cm = new SqlCommand("emp1", cn);
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.AddWithValue("@eid", o.EID);
cm.Parameters.AddWithValue("@name", o.NAME);
cm.Parameters.AddWithValue("@mark", 2);
return cm.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
cm.Dispose();
cn.Close();
cn.Dispose();
}
}
public int delete(BO o)
{
try
{
cm = new SqlCommand("emp1", cn);
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.AddWithValue("@eid", o.EID);
cm.Parameters.AddWithValue("@mark", 3);
return cm.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
cm.Dispose();
cn.Close();
cn.Dispose();
}
}
public DataTable view()
{
try
{
cm = new SqlCommand("emp1", cn);
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.AddWithValue("@mark", 4);
da = new SqlDataAdapter(cm);
da.Fill(dt);
return dt;
}
catch
{
throw;
}
finally
{
cm.Dispose();
cn.Close();
cn.Dispose();
da.Dispose();
}
}
}
Add another class on ur project, the class name is BAL.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
public class BAL
{
public int insert(BO o)
{
DAL p = new DAL();
try
{
return p.insert(o);
}
catch
{
throw;
}
finally
{
p = null;
}
}
public int update(BO o)
{
DAL p = new DAL();
try
{
return p.update(o);
}
catch
{
throw;
}
finally
{
p = null;
}
}
public int delete(BO o)
{
DAL p = new DAL();
try
{
return p.delete(o);
}
catch
{
throw;
}
finally
{
p = null;
}
}
public DataTable view()
{
DAL p = new DAL();
try
{
return p.view();
}
catch
{
throw;
}
finally
{
p = null;
}
}
}
design the the inter face like this :
design code is:
<%@ 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>
<style type="text/css">
.A
{
width:940px;
}
.B
{
width:360px;
text-align:right;
float:left;
padding-top:3px;
list-style:none;
}
.C
{
width:20px;
text-align:center;
float:left;
padding-top:3px;
list-style:none;
font-weight:bolder;
}
.D
{
width:560px;
text-align:left;
float:left;
padding-top:3px;
list-style:none;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
<center>
<div class="A">
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<li class="A" style="list-style:none; text-align:center"> <asp:Label ID="lb" runat="server"></asp:Label></li>
<li class="B">EID</li>
<li class="C">:</li>
<li class="D"><asp:TextBox ID="tb" runat="server"></asp:TextBox></li>
<li class="B">NAME</li>
<li class="C">:</li>
<li class="D"><asp:TextBox ID="tb1" runat="server" Width="200"></asp:TextBox></li>
<li class="B"> </li>
<li class="C"> </li>
<li class="D"><asp:Button ID="btn" runat="server" Text="Save" Width="80"
onclick="btn_Click" /> <asp:Button ID="Button1" runat="server" Text="Reset" Width="80" /></li>
<li class="B"> </li>
<li class="C"> </li>
<li class="D"><asp:GridView ID="gv" DataKeyNames="eid" runat="server" AllowPaging="True"
onpageindexchanging="gv_PageIndexChanging" onrowdeleting="gv_RowDeleting"
PageSize="2" onrowcancelingedit="gv_RowCancelingEdit"
onrowediting="gv_RowEditing" onrowupdating="gv_RowUpdating">
<Columns>
<asp:CommandField HeaderText="UPDATE" ShowEditButton="True" ShowHeader="True" />
<asp:CommandField HeaderText="DELETE" ShowDeleteButton="True"
ShowHeader="True" />
</Columns>
</asp:GridView></li>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</center>
</form>
</body>
</html>
C# CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
BO o = new BO();
BAL p = new BAL();
int i;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
view();
}
void view()
{
gv.DataSource = p.view();
gv.DataBind();
}
protected void btn_Click(object sender, EventArgs e)
{
o.EID = Convert.ToInt32(tb.Text);
o.NAME = tb1.Text;
i = p.insert(o);
if (i != 0)
{
lb.Text = "One record saved.";
lb.ForeColor = Color.Green;
view();
}
else
{
lb.Text = "Error on save.";
lb.ForeColor = Color.Red;
}
}
protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
view();
}
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
o.EID = Convert.ToInt32(gv.DataKeys[e.RowIndex].Value);
i = p.delete(o);
if (i != 0)
{
lb.Text = "One record deleted.";
lb.ForeColor = Color.Green;
view();
}
else
{
lb.Text = "Error on delete.";
lb.ForeColor = Color.Red;
}
}
protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gv.EditIndex = -1;
view();
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
view();
}
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
o.EID = Convert.ToInt32(gv.DataKeys[e.RowIndex].Value);
o.NAME = ((TextBox)gv.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
i = p.insert(o);
if (i != 0)
{
lb.Text = "One record updated.";
lb.ForeColor = Color.Green;
gv.EditIndex = -1;
view();
}
else
{
lb.Text = "Error on update.";
lb.ForeColor = Color.Red;
}
}
}
AWS online training
ReplyDeleteAzure DevOps online training
Azure online training
Chef online training
Data Guard online training
Data Modelling online training
Data Science online training
DevOps online training
Exchange Server online training
Google Cloud online training