Introduction
Here I
will explain about uses of 3-Tier architecture and how to create or
implement 3-tier architecture for our project in asp.net
Code for Business Entity Layer (BEL)
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace _3Tier
{
public class Insert_BEL
{
public string UserName{get; set;}
public string Password{get; set;}
}
}
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace _3Tier
{
public class Insert_BEL
{
public string UserName{get; set;}
public string Password{get; set;}
}
}
Code for Business Logic Layer (BLL)
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace _3Tier
{
public class Insert_BLL
{
Insert_DAL insert_dal = new Insert_DAL();
public string Insert(Insert_BEL insert_bel)
{
try
{
return insert_dal.Insert(insert_bel);
}
catch(Exception ex)
{
throw ex;
}
}
}
}
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace _3Tier
{
public class Insert_BLL
{
Insert_DAL insert_dal = new Insert_DAL();
public string Insert(Insert_BEL insert_bel)
{
try
{
return insert_dal.Insert(insert_bel);
}
catch(Exception ex)
{
throw ex;
}
}
}
}
Code for Data Access Layer (DAL)
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace _3Tier
{
public class Insert_DAL
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connstrname"].ConnectionString);
public string Insert(Insert_BEL insert_bel)
{
string OutPut = "";
try
{
SqlCommand cmd = new SqlCommand("Insert into Tbl_UserLoginDetails (UserName,Password) values (@UserName,@Password)", con);
cmd.Parameters.AddWithValue("@UserName", insert_bel.UserName);
cmd.Parameters.AddWithValue("@Password", insert_bel.Password);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
OutPut = "0";
}
catch (Exception ex)
{
OutPut = "1";
}
return OutPut;
}
}
}
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace _3Tier
{
public class Insert_DAL
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connstrname"].ConnectionString);
public string Insert(Insert_BEL insert_bel)
{
string OutPut = "";
try
{
SqlCommand cmd = new SqlCommand("Insert into Tbl_UserLoginDetails (UserName,Password) values (@UserName,@Password)", con);
cmd.Parameters.AddWithValue("@UserName", insert_bel.UserName);
cmd.Parameters.AddWithValue("@Password", insert_bel.Password);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
OutPut = "0";
}
catch (Exception ex)
{
OutPut = "1";
}
return OutPut;
}
}
}
Code for Presentation Layer (UI Layer)
In .aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_3Tier._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>Untitled Page</title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 69px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td class="style2">
UserName:</td>
<td>
<asp:TextBox ID="Txt_UserName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Password:</td>
<td>
<asp:TextBox ID="Txt_Password" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
<asp:Label ID="Lbl_Status" runat="server" Text=""></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
<asp:Button ID="Btn_Insert" runat="server" Text="Insert"
onclick="Btn_Insert_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
<!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>Untitled Page</title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 69px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td class="style2">
UserName:</td>
<td>
<asp:TextBox ID="Txt_UserName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Password:</td>
<td>
<asp:TextBox ID="Txt_Password" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
<asp:Label ID="Lbl_Status" runat="server" Text=""></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
<asp:Button ID="Btn_Insert" runat="server" Text="Insert"
onclick="Btn_Insert_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
In .aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace _3Tier
{
public partial class _Default : System.Web.UI.Page
{
Insert_BEL insert_bel = new Insert_BEL();
Insert_BLL insert_bll = new Insert_BLL();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Btn_Insert_Click(object sender, EventArgs e)
{
string OutPut = "";
insert_bel.UserName = Txt_UserName.Text;
insert_bel.Password = Txt_Password.Text;
OutPut = insert_bll.Insert(insert_bel);
if (OutPut == "0")
{
Lbl_Status.Text = "Data Inserted!";
}
else
{
Lbl_Status.Text = "Data NotInserted (Due to error)!";
}
}
}
}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace _3Tier
{
public partial class _Default : System.Web.UI.Page
{
Insert_BEL insert_bel = new Insert_BEL();
Insert_BLL insert_bll = new Insert_BLL();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Btn_Insert_Click(object sender, EventArgs e)
{
string OutPut = "";
insert_bel.UserName = Txt_UserName.Text;
insert_bel.Password = Txt_Password.Text;
OutPut = insert_bll.Insert(insert_bel);
if (OutPut == "0")
{
Lbl_Status.Text = "Data Inserted!";
}
else
{
Lbl_Status.Text = "Data NotInserted (Due to error)!";
}
}
}
}
In web.config
Remove or delete </connectionStrings>
Now Add below code
<connectionStrings>
<add name="connectionname" connectionString="Data Source=servername;Initial Catalog=databasename;Integrated Security=True;" providerName="System.Data.SqlClient"/>
</connectionStrings>
<add name="connectionname" connectionString="Data Source=servername;Initial Catalog=databasename;Integrated Security=True;" providerName="System.Data.SqlClient"/>
</connectionStrings>
Try it now :-)
Please give your valuable feedback
Thank U!

No comments:
Post a Comment