Wednesday 4 April 2012

How to use 3 Tier Architecture for Inserting and Retrieving Data || Binding of data to Gridview by using 3 Tier Architecture.

Here in this example i used the 3 Tier Architecture for Inserting and Retrieving Data from database and viewed in the Gridview. 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 in database and also create two stored procedures for inserting and viewing data from 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;

namespace DAL
{
    public class Dalcls
    {
        SqlHelper sql = new SqlHelper();
        public int UserSave(int ID, string Name, int Age, string Address, string Course)
        {
            Hashtable ht = new Hashtable();
            ht.Add("@S_Id", ID);
            ht.Add("@S_Name", Name);
            ht.Add("@Age ", Age);
            ht.Add("@Address", Address);
            ht.Add("@course", Course);
            int result = sql.ExecuteQuery("student_Insert", ht);
            return result;
        }
        public DataSet View()
        {
            Hashtable ht = new Hashtable();
            DataSet ds = sql.ExecuteProcudere("Student_view", ht);
            return ds;
        }
    }
}

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

namespace BAL
{
    public class Balcls
    {
        Dalcls dal = new Dalcls();
        public int UserSave(int ID, string Name, int Age, string Address, string Course)
        {
            return dal.UserSave(ID, Name, Age, Address, Course);
        }
        public DataSet View()
        {
            return dal.View();
        }
    }
}

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;


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

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int result = bal.UserSave(Convert.ToInt32(txtStudent_ID.Text), txtName.Text, Convert.ToInt32(txtAge.Text), txtAddress.Text, txtCourse.Text);
        if (result > 0)
        {
            string msg = "<script>alert('Inserted Successfully');</script>";
            ScriptManager.RegisterStartupScript(this, typeof(Control), "alertmsg", msg, false);
        }
    }
    protected void btnView_Click(object sender, EventArgs e)
    {
        DataSet ds =bal.View();

        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }
}


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=student;User ID=sa;Password=123"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="ConnectionString" value="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=student"/>
  </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 File :











15 comments:

  1. very good article for novice
    keep on..............
    thanks

    ReplyDelete
  2. grt article ..easy 2 understand
    thanks

    ReplyDelete
  3. where should i write sql helper class code?

    ReplyDelete
  4. what is the main useful to use sqlhelper class

    ReplyDelete
  5. nice article... very helpful.
    i am looking for editable nested gridview. if any chance please send me like this type of coding.

    ReplyDelete
  6. Hello Sir, Thanks for sharing the information. I want to know what is the use of Business Access layer in 3 tier architecture. We can directly call the methods written on Data Access and can perform the CRUD operations then why we require the Business Access Layer.

    ReplyDelete
  7. can u please post login page using 3 tier arch. for multiple users.

    ReplyDelete
  8. Easy to understand. Thank u. :)

    ReplyDelete
  9. Easy to understand. Thank u. :)

    ReplyDelete
  10. Your comment was published.

    ReplyDelete
  11. Hey, thanks for the blog article.Really looking forward to read more. Cool.
    tableau online training
    tableau training

    ReplyDelete