Inserting and Retreiving Data from Database using 3-tier Architecture in Asp.net || 3 Layer Architecture in Asp.net


For 3-tier Architecture first open visual studio add new project for DataAccess layer name as Dal in that take one class as Dalcls similarly add new project to the solution for Bussiness layer  name as Bal in that take one class as Balcls.
And add one website for Presentation Layer in that add one webpage take name as Presentation.aspx
In this article am taking one sqlhelper class for database connection .Refer sqlhelper class by Click here
And write the following code in Dal class:

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;
        }
    }
}

write the following code in Bal class:

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();
        }
    }
}

write the following code in Presentation Layer:

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(thistypeof(Control), "alertmsg", msg,false);
        }
    }
    protected void btnView_Click(object sender, EventArgs e)
    {
        DataSet ds =bal.View();

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



Output File :



0 comments:

Post a Comment