Follow by Email

Monday, 24 September 2012

Three tier architecture in asp.net example of inserting records

The 3-Tier architecture contains 3 layers
1.    Application Layer or Presentation Layer 
2.    Business Access Layer(BAL) or Business Logic Layer(BLL) 
3.    Data Access Layer(DAL)
 
Application Layer/ Presentation Layer:


 
 
 Business Access Layer(BAL) or Business Logic Layer(BLL) :
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 BAL
/// </summary>
public class BAL
{
    string _fname, _lname;
    DAL d1 = new DAL();
      public BAL()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public string fname
    {
        get
        {
            return this._fname;
        }
        set
        {
            this._fname = value;
        }

    }
    public string lname
    {
        get
        {
            return this._lname;
        }
        set
        {
            this._lname = value;
        }

    }

    public void insert_BAL(string Str_Pro)
    {
        d1.insert_records(Str_Pro, this);
    }


}

STORED PROCEDURE USED:

CREATE PROCEDURE [dbo].[insert_record]

@fname varchar(50),
@lname varchar(50)

AS
BEGIN

INSERT INTO test_tab2 (fname,lname) values(@fname,@lname)

END
 
 
 Data Access Layer(DAL):

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 System.Data.SqlClient;

/// <summary>
/// Summary description for DAL
/// </summary>
public class DAL
{
    SqlConnection con = new SqlConnection();
    public DAL()
    {
        con = new SqlConnection("Data Source=EVEREST5026;Initial Catalog=sample;Integrated Security=True");
    }

    public void insert_records(string Str_Pro, BAL b1)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand(Str_Pro, con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter param1, param2;

        param1 = new SqlParameter("@fname", SqlDbType.VarChar);
        param1.Value = b1.fname;
       // param1.Direction = ParameterDirection.Input;
        cmd.Parameters.Add(param1);

        param2 = new SqlParameter("@lname", SqlDbType.VarChar);
        param2.Value = b1.lname;
       // param2.Direction = ParameterDirection.Input;
        cmd.Parameters.Add(param2);

        cmd.ExecuteNonQuery();
        con.Close();

    }

}


CODE BEHIND PAGE:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;

public partial class index : System.Web.UI.Page
{
    BAL b1 = new BAL();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        b1.fname = TextBox1.Text;
        b1.lname = TextBox2.Text;
        b1.insert_BAL("insert_record");
    }
}

 

0 comments:

Post a Comment


                                                            

 
Design by Abhinav Ranjan Sinha