CRUD Operation Using 3-Tier Architecture In ASP.NET

In this tutorial we have learn CRUD Operation Using 3-Tier Architecture In ASP.NET  and how to Bind Grid view to display data using the SQL Server Database

Step 1 : First create sql Database with table user_master

CRUD Operation Using 3-Tier Architecture In ASP.NET

 

Step 2: 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; }
}

Step 3: Create Class BAL.CS

public class BAL
{
SqlConnection con = new SqlConnection( @”Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Program file\3tier\3tier\App_Data\Database1.mdf;IntegratedSecurity=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;
}
}

Step 4 : Create asp page Reg.aspx

CRUD Operation Using 3-Tier Architecture In ASP.NET

 

Step 5 : add code to 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 web application and see output :

CRUD Operation Using 3-Tier Architecture In ASP.NET

SEE MORE