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
public class DAL
{
public int id { get; set; }
public string uname { get; set; }
public string uaddress { get; set; }
public string uhobby { get; set; }
}
{
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 +
{
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;
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 :
{
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 :
}
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 :
See Other Tutorial :
* AngularJS With ASP.NET MVC
* Convert Rows to columns using 'Pivot' in SQL Server
* Mvc Registration page With user exist using Ajax method
* MVC 4 How to Perform Insert Update Delete Edit Select Operation
* MVC4 Edit,update,Delete,cancel inside gridview using sql database
* MVC 4 Gridview To Display Data Using SQL Server Database With Simple code
* Login page in asp.net Mvc4 Web application
* Mvc4 How to bind Dropdown List using Sql Database
* Gridview find control in asp.net
* AngularJS With ASP.NET MVC
* Convert Rows to columns using 'Pivot' in SQL Server
* Mvc Registration page With user exist using Ajax method
* MVC 4 How to Perform Insert Update Delete Edit Select Operation
* MVC4 Edit,update,Delete,cancel inside gridview using sql database
* MVC 4 Gridview To Display Data Using SQL Server Database With Simple code
* Login page in asp.net Mvc4 Web application
* Mvc4 How to bind Dropdown List using Sql Database
* Gridview find control in asp.net
Sign up here with your email
4 comments
Write commentsthis is a nice site.
Replywow, its really simple and great code working well..
Replythanks for posting this code.
Replygreat code working good
ReplyConversionConversion EmoticonEmoticon