Asp.Net 3- tier Architecture with Crude operation

Asp.Net 3- tier Architecture with Crude operation


Introduction:
This article explains how to insert,Update,Delete,Edit , Select Operation  and how to Bind Grid view to display data using the SQL Server Database. 

Create new Web Project
Create Database In SQL Server  and give table name  user_master
























  Create Class DAL.CS :

 public class DAL
    {
        public int  id { get; set; }
        public string uname { get; set; }
        public string uaddress { get; set; }
        public string uhobby { get; set; }
    }

Create Class BAL.CS :

 public class BAL
    {
        SqlConnection con = new SqlConnection( @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Program file\3tier\3tier\App_Data\Database1.mdf;Integrated Security=True;User Instance=True");

        public void insert_data(DAL da)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand();

            cmd = new SqlCommand("insert into user_master values('" + da.uname + "','" + da.uaddress + "','"   + da.uhobby + "')", con);
            cmd.ExecuteNonQuery();
            con.Close();
        }
        public DataTable edit_data(DAL da)
        {
            DataTable dt = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter("select * from user_master where uname='" +               da.uname + "'", con);
            sda.Fill(dt);
            return dt;
        }
        public void delete_data(DAL da)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("delete from user_master where uname='" + da.uname +  "'", con);
            cmd.ExecuteNonQuery();
            con.Close();
        }

        public void update_data(DAL da)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("update user_master set uaddress='" + da.uaddress +     
 "',uhobby='" + da.uhobby + "' where uname='" + da.uname + "'", con);
            cmd.ExecuteNonQuery();
            con.Close();
        }
        public DataTable bind_gridview(DAL da)
        {
            DataTable dt = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter("select * from user_master", con);
            sda.Fill(dt);
            return dt;
       }
 }

Now Create Reg.aspx  Page :























 

 

 


Now  you you can add code in code behind file as following.

reg.aspx.cs :

  public partial class reg : System.Web.UI.Page
    {
        DAL da = new DAL();
        BAL ba = new BAL();
        DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            GridView();
         
        }
        public void GridView()
        {
           dt=ba.bind_gridview(da);
           GridView1.DataSource = dt;
           GridView1.DataBind();
          
        }

        protected void btn_submit_Click(object sender, EventArgs e)
        {
            da.uname = txtname.Text;
            da.uaddress = txtadress.Text;
            da.uhobby = Chhobby.SelectedItem.Text;
            ba.insert_data(da);
            Response.Write("data inserted............");
            GridView();

        }

        protected void btn_edit_Click(object sender, EventArgs e)
        {
            da.uname = txtname.Text;
            dt = ba.edit_data(da);
            txtadress.Text = dt.Rows[0]["uaddress"].ToString();
            Chhobby.SelectedValue = dt.Rows[0]["uhobby"].ToString();

            GridView();
        }

        protected void btn_delete_Click(object sender, EventArgs e)
        {
            da.uname = txtname.Text;
            da.uaddress = txtadress.Text;
            da.uhobby = Chhobby.SelectedItem.Text;
            ba.delete_data(da);
            GridView();

        }

        protected void btn_update_Click(object sender, EventArgs e)
        {
            da.uname = txtname.Text;
            da.uaddress = txtadress.Text;
            da.uhobby = Chhobby.SelectedItem.Text;
            ba.update_data(da);
            Response.Write("data updated...");
            GridView();
           
        }

    } 

Run Program : 




 

First

4 comments

Write comments
Chetan
AUTHOR
2 August 2015 at 22:59 delete

this is a nice site.

Reply
avatar
Unknown
AUTHOR
28 February 2016 at 03:10 delete

wow, its really simple and great code working well..

Reply
avatar
Unknown
AUTHOR
28 February 2016 at 03:11 delete

thanks for posting this code.

Reply
avatar
Anonymous
AUTHOR
3 March 2016 at 21:57 delete

great code working good

Reply
avatar