Tuesday, September 6, 2011

How to Use 3-Tier Architecture in ASP.NET with C#

How to Use 3-Tier Architecture in ASP.NET with C#


It contain three types of layers.These layers are described below.

1. Data layer
2. Busines layer
3. Applicaion layer 
  • Data later is also the class which gets the data from the business layer and sends it to the database or gets the data from the database and sends it to the business layer.
  • Business layer is the class where we write the functions which get the data from the application layer and passes through the data access layer.
  • Application layer is the form where we design using the controls like textbox, labels, command buttons etc.
Here the example follow by step by step :-

1. In SQL Query analyzer generate these steps.Its use to create a new table and also column names with datatypes likes sname,age,address.

Here the table name is sample1

create table sample1(sname varchar(20),age int,address varchar(20))

2.Similarly, how to use the insert the records, update the records and delete the records by using simple stored procedure.


Stored procedures are created with the CREATE PROCEDURE statement. The syntax is shown below.

CREATE PROCEDURE procedure_name
@param data_type = default_value,
@param data_type = default_value,
@param data_type = default_value
AS
-- statements for procedure here


 
a. Its for Insert the Record:-

create procedure prd_sample1
(
@sname varchar(20),
@age int,
@address varchar(20)
)
as
begin
insert into sample1 values(@sname,@age,@address)
end

b. Its for delete the Record:-

create procedure prd_sample2
(
@sname varchar(20)
)
as
begin
delete from sample1 where sname=@sname;
end

a. Its for Modify the Record:-


create
procedure prd_sample3
(
@sname varchar(20),
@age int,
@address varchar(20)
)
as
begin
update sample1 set age=@age,address=@address where sname=@sname
end

2.Create a New Website in .Net Application :

In the Wesbite must have one Default.aspx and Default.aspx.cs

In Default.aspx :


Specify the connection string in web.config file

 
<configuration>
<appSettings>
<add key="dbconnection" value="server=server name; Initial Catalog= databasename; Integrated Security=true"/>
</appSettings>
<connectionStrings/>

Source Code:

Create Two Folder in Solution Explorer: BOL and DAL in App Code Folder

Insert DAL Folder: Use right click add class file (.cs)


Create dataaccesslayer.cs file:


using System;
using System.Data;
using System.Data.SqlClient;
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 dataaccesslayer
/// </summary>
namespace datalayer
{

/// <summary>
/// Summary description for parameter
/// </summary>
public class parameter
{
private IDbCommand cmd = new SqlCommand();
private string strConnectionString = "";
private bool handleErrors = false;
private string strLastError = "";

public parameter()
{
strConnectionString = System.Configuration.ConfigurationManager.AppSettings["dbconnection"].ToString();
SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = strConnectionString;
cmd.Connection = cnn;
cmd.CommandType = CommandType.StoredProcedure;
}


public int ExecuteNonQuery()
{
int i = -1;
try
{
this.Open();
i = cmd.ExecuteNonQuery();
this.Close();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}

return i;
}


public int ExecuteNonQuery(string commandtext)
{
int i = -1;
try
{
cmd.CommandText = commandtext;
i = this.ExecuteNonQuery();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}

return i;
}


public string CommandText
{
get
{
return cmd.CommandText;
}
set
{
cmd.CommandText = value;
cmd.Parameters.Clear();
}
}

public IDataParameterCollection Parameters
{
get
{
return cmd.Parameters;
}
}

public void AddParameter(string paramname, object paramvalue)
{
SqlParameter param = new SqlParameter(paramname, paramvalue);
cmd.Parameters.Add(param);
}

public void AddParameter(IDataParameter param)
{
cmd.Parameters.Add(param);
}


public string ConnectionString
{
get
{
return strConnectionString;
}
set
{
strConnectionString = value;
}
}

private void Open()
{
cmd.Connection.Open();
}

private void Close()
{
cmd.Connection.Close();
}

public bool HandleExceptions
{
get
{
return handleErrors;
}
set
{
handleErrors = value;
}
}

public string LastError
{
get
{
return strLastError;
}
}

public void Dispose()
{
cmd.Dispose();
}



}
}

In BOL:

Create businesslayer.cs file:

using System;
using System.Data;
using System.Data.SqlClient;
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 datalayer;
/// <summary>
/// Summary description for businesslayer
/// </summary>
///
namespace business.BOL
{
public class business1
{
private string txtname;// using prop keyword press tabs

public string sname
{
get { return txtname; }
set { txtname = value; }
}
private int txtage;

public int age
{
get { return txtage; }
set { txtage = value; }
}
private string txtaddress;

public string address
{
get { return txtaddress; }
set { txtaddress = value; }
}
public static int insert(business1 b)
{
parameter p = new parameter();
p.Parameters.Add(new SqlParameter("@sname", b.sname));
p.Parameters.Add(new SqlParameter("@age", b.age));
p.Parameters.Add(new SqlParameter("@address", b.address));
return p.ExecuteNonQuery("prd_sample1");
}
public static int delete(business1 d)
{
parameter bd=new parameter();
bd.AddParameter("sname", d.sname);
return bd.ExecuteNonQuery("prd_sample2");
}
public static int update(business1 u)
{
parameter bu = new parameter();
bu.Parameters.Add(new SqlParameter("@sname", u.sname));
bu.Parameters.Add(new SqlParameter("@age", u.age));
bu.Parameters.Add(new SqlParameter("@address", u.address));
return bu.ExecuteNonQuery("prd_sample3");
}
}
}




In form design: Default.aspx.cs

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;
using business.BOL;

public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)---------> Insert the Record
{
business1 b1 = new business1();
b1.sname = txtname.Text;
b1.age = Convert.ToInt32(txtage.Text);
b1.address = txtaddress.Text;
business1.insert(b1);

}
protected void Button2_Click(object sender, EventArgs e)------>To Delete the Record
{
business1 bd = new business1();
bd.sname = txtname.Text;
business1.delete(bd);

}
protected void Button3_Click(object sender, EventArgs e)------>To Modify the Record
{
business1 bu = new business1();
bu.sname = txtname.Text;
bu.age = Convert.ToInt32(txtage.Text);
bu.address = txtaddress.Text;
business1.update(bu);

}
}


No comments:

Post a Comment