Friday, 27 December 2013

How to Implement 3-Tier architecture in C# ASP.NET

When we create any .net application and want to seperate our logic from user interface than we all think of 3-tier architecture..Than another question raise in our mind how to implement 3 tier architecture in asp.net???

Solution :-
Start a new website project.
Design you page with 3 labels, 3 textboxes and 1 button.
Add sub-folders and class objects within the App_code folder as shown below :-



Code for the add button is something like this :-
protected void btnAdd_Click(object sender, EventArgs e)
{
    CustomerBAL customer = new CustomerBAL();
    try
    {
        if (customer .Insert(txtCustomerID.Text, txtFirstName.Text, txtLastName.Text) > 0)
        {
            lblMessageStatus.Text = "Record inserted successfully.";
        }
        else
        {
            lblMessageStatus.Text = "Record not inserted, some error occured.";
        }
    }
    catch (Exception ex)
    {
        lblMessageStatus.Text = ex.Message;
    }
    finally //Always Executable code here
    {
        customer = null;
    }
} 
Create a class with name 'CustomerBAL'.This will work as your Business layer.
Code for CustomerBAL.cs
using System;
using System.Collections.Generic;
using System.Web;

public class CustomerBAL
{
    public CustomerBAL()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public int Insert(string CustomerID, string FirstName, string LastName)
    {
        CustomerDAL customerDAL=new CustomerDAL();
        try
        {
            return customerDAL.Insert(CustomerID, FirstName, LastName);
        }
        catch
        {
            throw;
        }
        finally
        {
            customerDAL = null;
        }
    }
} 
Create a Class with name 'CustomerDAL'.This will work as Your Data Access Layer.
Code for CustomerDAL.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Data.SqlClient;
using System.Data;

public class CustomerDAL
{
    public CustomerDAL()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public int Insert(string CustomerID, string FirstName, string LastName)
    {
        //declare SqlConnection and initialize it to the settings in the section 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 = 28;
            Command.Parameters["@CustomerID"].Value = CustomerID;

            Command.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.VarChar));
            Command.Parameters["@FirstName"].Direction = ParameterDirection.Input;
            Command.Parameters["@FirstName"].Size = 28;
            Command.Parameters["@FirstName"].Value = FirstName;

            Command.Parameters.Add(new SqlParameter("@LastName", SqlDbType.VarChar));
            Command.Parameters["@LastName"].Direction = ParameterDirection.Input;
            Command.Parameters["@LastName"].Size = 28;
            Command.Parameters["@LastName"].Value = LastName;

            //open the database connection
            Conn.Open();
            //execute the command
            return Command.ExecuteNonQuery();
        }
        catch
        {
            throw;
        }
        finally //Connection will end here
        {
            Command.Dispose();
            Conn.Dispose();
        }
    }
}
Set the connection string on the web.config file.
Run the Project.



That’s it!!…..Happy Programming...

No comments:

Post a Comment