Wednesday 4 April 2012

3 Tier Architectute

How to use 3 Tier Architecture for Inserting Data to Database.

 

Here in this example for 3 Tier Architecture for Inserting Data to database. 3 tier architecture is secured, easily manageable,and highly understandable. 


3-Tier architecture consists of
1) UI or Presentation Layer

2) Business Access Layer or Business Logic Layer

3) Data Access Layer

The presentation tier contains the UI (User Interface) elements of the site, and includes all the logic that manages the interaction between the visitor and the client’s business.

The business tier receives requests from the presentation tier and returns a result to the presentation tier depending on the business logic it contains.

The data tier  is responsible for storing the application’s data and sending it to the business tier when requested.

To implement this concept you need to follow the below steps

Step1 :
First you need to design a table in Sql Database to to save the records into database.
Step2:
Create a new Asp.net website in Visual Studio.Go to Solution Explorer and then right click on your website add new project for DataAccess layer  name it as Dal and select a class as Dalcls.cs similarly add new project  for Bussiness layer name it as Bal and select a class as Balcls.cs in it. Add New Item to the solution and select a Webform and name it as Presentation.aspx.

For this concept i have taken a SqlHelper class for database connection. For SqlHelper class Click Here
 
Step3:
Now open the Dalcls.cs  page and write the following source code.

Dalcls.cs 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
   public class Dalcls
    {
        SqlHelper sql = new SqlHelper();
        public int PresentationAdd(int Id, string Name,int Age, string Address,string Email)
        {
            Hashtable ht = new Hashtable();
            ht.Add("@EmpID",Id);
            ht.Add("@EmpName",Name);
            ht.Add("@EmpAge", Age);
            ht.Add("@EmpAddress", Address);
            ht.Add("@EmailId",Email);
            int result = sql.ExecuteQuery("EmployeeDetails", ht);
            return result;
        }
    }
}


Step4:
Now open the Balcls.cs  page and write the following source code.
Balcls.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAL;

namespace BAL
{
    public class Balcls
    {
        Dalcls ds = new Dalcls();
        public int PresentationAdd(int Id, string Name, int Age, string Address, string Email)
        {
            return ds.PresentationAdd(Id, Name,Age,Address,Email);
        }
    }
}

Step5:
Now open the Presentation.aspx.cs page and write the following source code.
Presentation.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL;
using System.Data;
using System.Data.SqlClient;

public partial class Presentation : System.Web.UI.Page
{
    Balcls bs = new Balcls();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int result = bs.PresentationAdd(Convert.ToInt32(txtEmpId.Text), txtname.Text, Convert.ToInt32(txtAge.Text), txtAddress.Text, txtEmail.Text);
        if (result > 0)
        {
            string msg = "<script>alert('Inserted Successfully');</script>";
            ScriptManager.RegisterStartupScript(this, typeof(Control), "alertmsg", msg, false);
        }
    }
}

Step6:
Now open the Web.config File and write the following source code.

Web.config File
<configuration>
  <connectionStrings>
    <add name="employeeConnectionString" connectionString="Data Source=sridhar;Initial Catalog=employee;User ID=sa;Password=123"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="ConnectionString" value="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=employee"/>
  </appSettings>
       <system.web>
              <compilation debug="true" targetFramework="4.0">
              </compilation>
       </system.web>
</configuration>


Step7:
Now build the Solution and Debug it for the output.

Output :




3 comments:

  1. wrong example !!!!

    ReplyDelete
  2. data layer wont contain any field ....
    rather business layer will contain fields of database !! not data layer

    ReplyDelete
    Replies
    1. I think you are not aware of the 3-tier application. Business layers consists of only logic's and data layers consists of database manipulations.

      Delete