Sunday 28 October 2012

New Three Tire Architecture in Asp.Net using C Sharp


Basically three tire architecture contains  three different layers:
1.)Application Layer or Presentation Layer
2.)Bussiness logic layer or Bussiness Access layer(BAL)
3.)Data logic layer or Data Access Layer(DAL)
Use of 3 tire architecture:
1.)  Easy to understand
2.) Easy to maintain ,easy to manipulate and easy to  modify.
Here  I have explained   step by step  so that anyone can understand the logic in simple way.
Apllication Layer:
It is nothing but just a design page means  how will   your page look . You can design your page in your own way . Design coding is given below.
Home.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home" %>

<!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>Home</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:Label ID="Label1" runat="server" Text="username"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
        <asp:Label ID="Label3" runat="server" Text="First Name"></asp:Label>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
        <asp:Label ID="Label4" runat="server" Text="Last Name"></asp:Label>
        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
        <asp:Label ID="Label5" runat="server" Text="Email"></asp:Label>
        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br />
        <asp:Label ID="Label6" runat="server" Text="Phone No"></asp:Label>
        <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox><br />
        <asp:Label ID="Label7" runat="server" Text="Location"></asp:Label>
        <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox><br />
       
        <asp:Button ID="Button1" runat="server" Text="Register"
            onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

After this will discuss about Bussiness logic layer.
This layer is used as interface between the aplliacrion layer and Data Access layer.
How to add this layer.
Right click on your project application-> add new items -> select class file-> give it name as BAL.cs.
One folder created with App_code name.
Again right click on App_code -> select new items -> select class file and give a name BEL.cs.

Code For BEL.cs:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for BEL
/// </summary>
public class BEL
{
      public BEL()
      {
            //
            // TODO: Add constructor logic here
            //
    }
    #region Variables
    /// <summary>
    /// User Registration Variables
    /// </summary>
    private string _UserName;
    private string _Password;
    private string _FirstName;
    private string _LastName;
    private string _Email;
    private string _Phoneno;
    private string _Location;
    
    #endregion
    public string UserName
    {
        get { return _UserName; }
        set { _UserName = value; }
    }
    public string Password
    {
        get { return _Password; }
        set { _Password = value; }
    }
    public string FirstName
    {
        get { return _FirstName; }
        set { _FirstName = value; }
    }
    public string LastName
    {
        get { return _LastName; }
        set { _LastName = value; }
    }
    public string Email
    {
        get { return _Email; }
        set { _Email = value; }
    }
    public string Phoneno
    {
        get { return _Phoneno; }
        set { _Phoneno = value; }
    }
    public string Location
    {
        get { return _Location; }
        set { _Location = value; }
    }
}

Code for  BAL.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for BAL
/// </summary>
public class BAL
{
      public BAL()
      {
            //
            // TODO: Add constructor logic here
            //
    }
    #region Insert UserInformationDetails
    public string InsertUserDetails(BEL objUserDetails)
    {
        DAL _objUserDAL = new DAL();
        try
        {
            return _objUserDAL.InsertUserInformation(objUserDetails);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            _objUserDAL = null;
        }
    }
    #endregion
}

Data Access Layer:
Data Access layer contains method , is used to connect with DataBase and perform insert ,update and delete operations.
Code for DAL.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for DAL
/// </summary>
public class DAL
{
      public DAL()
      {
            //
            // TODO: Add constructor logic here
            //
      }
    string ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
    public string InsertUserInformation(BEL objBELUserDetails)
    {
        SqlConnection con = new SqlConnection(ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("sp_user_registration", con);
        cmd.CommandType = CommandType.StoredProcedure;
        try
        {
            cmd.Parameters.AddWithValue("@UserName", objBELUserDetails.UserName);
            cmd.Parameters.AddWithValue("@Password", objBELUserDetails.Password);
            cmd.Parameters.AddWithValue("@FirstName", objBELUserDetails.FirstName);
            cmd.Parameters.AddWithValue("@LastName", objBELUserDetails.LastName);
            cmd.Parameters.AddWithValue("@Email", objBELUserDetails.Email);
            cmd.Parameters.AddWithValue("@PhoneNo", objBELUserDetails.Phoneno);
            cmd.Parameters.AddWithValue("@Location", objBELUserDetails.Location);
           
            //cmd.Parameters.Add("@ERROR", SqlDbType.Char, 500);
            //cmd.Parameters["@ERROR"].Direction = ParameterDirection.Output;
            cmd.ExecuteNonQuery();
         // string strMessage = (string)cmd.Parameters["@ERROR"].Value;
            string strMessage = "Any Message";
            con.Close();
            return strMessage;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            cmd.Dispose();
            con.Close();
            con.Dispose();
        }
    }
}

Now Data  Access layer and Bussiness logic layer is ready. This time for coding inside the Home.aspx.cs page.
Code for Home.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Home : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string output = string.Empty;
        BEL _objbel = new BEL();
        _objbel.UserName = TextBox1.Text;
        _objbel.Password = TextBox2.Text;
        _objbel.FirstName = TextBox3.Text;
        _objbel.LastName = TextBox4.Text;
        _objbel.Email = TextBox5.Text;
        _objbel.Phoneno = TextBox6.Text;
        _objbel.Location = TextBox7.Text;
       
        BAL _objbal = new BAL();
        output = _objbal.InsertUserDetails(_objbel);
    }
}

Now all the process is completed, so it’s time to run the project. If any problem regarding this project you can contact me at sushilct86@gmail.com or give a comment . 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.