How To Use
3-Tire Architecture In Asp.net Using
Store Procedure:-
Introduction
3-Tire architecture is very important concept in asp.net for more security ,It is very simple to use.
3-Tire architecture is very important concept in asp.net for more security ,It is very simple to use.
Generally 3-Tier architecture generally contains 3-parts.
1>UI or Presentation Layer.
2>Business Access Layer (BAL) or Business Logic Layer.
--:Description About 3Tire Application:--
1>Presentation Layer
(UI)
Presentation layer (UI) cotains pages like .aspx or windows form where user can input their data.
2>Business Access Layer (BAL) or Business Logic Layer
Business Access Layer(BAL) contains business logic, validations or calculations related with the data, if needed. Business Access Layer(BAL) calls to Data Access Layer.
3>Data Access Layer (DAL)
Data Access Layer(DAL) contains methods that helps business layer to connect the data and perform required action, might be returning data or manipulating data (insert, update, delete etc).
Presentation layer (UI) cotains pages like .aspx or windows form where user can input their data.
2>Business Access Layer (BAL) or Business Logic Layer
Business Access Layer(BAL) contains business logic, validations or calculations related with the data, if needed. Business Access Layer(BAL) calls to Data Access Layer.
3>Data Access Layer (DAL)
Data Access Layer(DAL) contains methods that helps business layer to connect the data and perform required action, might be returning data or manipulating data (insert, update, delete etc).
Now Design 3-Tier Architecture Like This:-
Step-1:-
First Make a
table like this in your sqlserver data base:-(Suppose Table name is rabi)
Name
|
varchar(50)
|
Ads
|
varchar(50)
|
Step-2:-
Follow this step to Create Store Procedure :-
v Go
to Server Explorer in your .net
application and select storeprocedure than rightclick over this and -select Add New Store Procedure
v Create a store procedure in Your
sqlserver data base(Suppose name of store procedure is “insertpro”)
Code
Of Create Store Procedure :-
CREATE PROCEDURE insertpro
@name varchar(50),@ads varchar(50)
AS
insert into rabi(name,ads)
values(@name,@ads)
RETURN
Than save it
by key Ctrl+S
,After save Your Store
Procedure Show like this:-
ALTER PROCEDURE insertpro
@name varchar(50),@ads varchar(50)
AS
insert into rabi(name,ads)
values(@name,@ads)
RETURN
Step-3:-
v Go to
the web.config file in Your application
to declare your Connection String
like this:-
<connectionStrings>
<add name="conn" connectionString="Data
Source=xyz;Initial Catalog=xxx;Integrated Security=True"/>
</connectionStrings>
Step-4:-
Code of data access layer.
Go to your solution Explorer select
your project name and right click here than add two class file
within App_Code and provide
name “ regbal.cs “and “regdal.cs”;
v Now double click Over regdal.cs(data access layer) file and write
this code:-
Ø Import to this at
top of your application in code behind page:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// Summary description for regbal
/// </summary>
public class regdal
{
string connStr = ConfigurationManager.ConnectionStrings["conn"].ToString();
public regdal()
{
//
// TODO: Add constructor logic here
//
}
public int Insert(string name, string
ads)
{
SqlConnection conn = new
SqlConnection(connStr);
conn.Open();
SqlCommand dCmd = new
SqlCommand("Insertpro",
conn);
dCmd.CommandType = CommandType.StoredProcedure;
try
{
dCmd.Parameters.AddWithValue("@name",name);
dCmd.Parameters.AddWithValue("@ads",
ads);
return dCmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
dCmd.Dispose();
conn.Close();
conn.Dispose();
}
}
}
Step-5:-
Code of business access layer.
v Now double click Over regbal.cs(data access layer) file and write
this code:-
Ø Import to this at
top of your application in code behind page:-
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for regdal
/// </summary>
public class regbal
{
public regbal()
{
//
// TODO: Add constructor logic here
//
}
public int Insert(string name, string
ads)
{
regdal pBAL = new regdal();
try
{
return pBAL.Insert(name, ads);
}
catch
{
throw;
}
finally
{
pBAL = null;
}
}
}
Step-6:-
Presentation Layer(UI):-
Design Code Of Presentation Layer:
Ø Go to
design page and design like this code is given below :-
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 27%;
border: 1px solid #FF0000;
}
#form1
{
width: 1012px;
height: 183px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<%-- <p align="center">Registration bind
</p>--%>
<table class="style1">
<tr>
<td
align="center"
colspan="2"
style="color: #FF0000; font-weight: bold; font-size: medium">
==:Welcome to Presentation layer Page:==</td>
</tr>
<tr>
<td>
Name</td>
<td>
<asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Ads</td>
<td>
<asp:TextBox ID="TextBox2"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label1" runat="server"
Text="Label"></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
<td
align="center"
colspan="2">
<asp:Button ID="Button1"
runat="server"
onclick="Button1_Click"
Text="Summit"
BackColor="#FF9966" ForeColor="Blue"
/>
</td>
</tr>
</table>
<p align="center"> </p>
</div>
<br />
<br />
<br />
<p>
</p>
</form>
</body>
</html>
Step-7:-
Code Of the Presentation layer Plz write this code in code behind form:-
Ø Import to this at
top of your application in code behind page:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class _3layer :
System.Web.UI.Page
{
string ss = "Your
Record Saved Sucessfully..Thanks...";
SqlConnection conn = new
SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
protected void
Page_Load(object sender, EventArgs e)
{
}
protected void
Button1_Click(object sender, EventArgs e)
{
if (!Page.IsValid)
return;
int inserta = 0;
regbal
bal = new regbal();
string name = TextBox1.Text;
string ads = TextBox2.Text;
try
{
inserta = bal.Insert(name, ads);
if (inserta > 0)
Response.Write("<script>alert('"+ss+"')</script>");
else
Label1.Text = "null";
}
catch (Exception
ee)
{
Label1.Text = "sorry..try
again...";
}
finally
{ bal
= null; }
}
}
No comments:
Post a Comment