In 3 tier programming architecture, the application development is broken into three main parts; these are: Presentation layer(PL), Business Acess Layer(BAL) and Data Access Layer(DAL). These separations ensure independent development of those layers such that a change in one does not affect the other layers. For instance a change in the logic of the DAL layer does not affect the the BAL nor the presentation layer. It also makes testing easier in that a whole layer can be replaced with a stub object.
For example instead of testing the whole application with an actual connection to database, the DAL can be replaced with a stub DAL for testing purposes. The DAL deals with actual database operations. These operations are inserting, updating, deleting and viewing. The BAL deals with the business logic for a particular operation. The PL has the user controls for interacting with the application.
3-Tier programming architecture also enables re-use of code and provides a better way for multiple developers to work on the same project simultaneously.
- Start a new website project
- Design you page as shown below,
- Add sub-folders and class objects within the App_code folder as shown below,
- Code for the add button is shown below,
protected void cmdAdd_Click(object sender, EventArgs e)
{
CustomerBAL cBal = new CustomerBAL();
try
{
if (cBal.Insert(txtCustomerID.Text, txtFirstName.Text, txtLastName.Text) > 0)
{
lblMessageLine.Text = "Record inserted successfully.";
}
else
{
lblMessageLine.Text = "Record not inserted.";
}
}
catch (Exception ex)
{
lblMessageLine.Text = ex.Message;
}
finally
{
cBal = null;
}
} - code for CustomerBAL.cs
using System;
using System.Collections.Generic;
using System.Web;
///
/// Summary description for CustomerBAL
///
public class CustomerBAL
{
public CustomerBAL()
{
//
// TODO: Add constructor logic here
//
}
public int Insert(string CustomerID, string FirstName, string LastName)
{
CustomerDAL cDal=new CustomerDAL();
try
{
return cDal.Insert(CustomerID, FirstName, LastName);
}
catch
{
throw;
}
finally
{
cDal = null;
}
}
} - Code for CustomerDAL.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Data.SqlClient;
using System.Data;
///
/// Summary description for CustomerDAL
///
public class CustomerDAL
{
public CustomerDAL()
{
//
// TODO: Add constructor logic here
//
}
public int Insert(string CustomerID, string FirstName, string LastName) public int Insert(string CustomerID, string FirstName, string LastName)
{
//declare SqlConnection and initialize it to the settings in thesection of the web.config
SqlConnection Conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString);
//===============================
//prepare the sql string
string strSql = "insert into t_Customers(CustomerID,FirstName,LastName) ";
strSql = strSql + "values(@CustomerID,@FirstName,@LastName)";
//declare sql command and initalize it
SqlCommand Command = new SqlCommand(strSql, Conn);
//set the command type
Command.CommandType = CommandType.Text;
try
{
//define the command parameters
Command.Parameters.Add(new SqlParameter("@CustomerID", SqlDbType.VarChar));
Command.Parameters["@CustomerID"].Direction = ParameterDirection.Input;
Command.Parameters["@CustomerID"].Size = 20;
Command.Parameters["@CustomerID"].Value = CustomerID;
Command.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.VarChar));
Command.Parameters["@FirstName"].Direction = ParameterDirection.Input;
Command.Parameters["@FirstName"].Size = 25;
Command.Parameters["@FirstName"].Value = FirstName;
Command.Parameters.Add(new SqlParameter("@LastName", SqlDbType.VarChar));
Command.Parameters["@LastName"].Direction = ParameterDirection.Input;
Command.Parameters["@LastName"].Size = 25;
Command.Parameters["@LastName"].Value = LastName;
//open the database connection
Conn.Open();
//execute the command
return Command.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
Command.Dispose();
Conn.Dispose();
}
}
} - Set the connection string on the web.config file
- Run the project
0 comments:
Post a Comment