Insert, Update, Delete In GridView Using ASP.Net C#

In this Tutorial we can learn Insert, Update, Delete In GridView Using ASP.Net C#. Here we can use the gridview control and sql server database to display data in grid

Step 1 : First  need to Create new asp.net Web Project

Step 2 : Create Sql database with table name “user_master

Step 3 : Now create webform2.aspx 

Step 3 : Now you can need to add class DAL.cs  this class is for data acces layer here you can define database structue for accessing data from the database.

Create Class DAL :

public class DAL
{
public int id { get; set; }
public string name { get; set; }
public string city { get; set; }

}

Step 4 :

After creating the DAL class now you can need to add BAL.CS class this class known as busseness access layer or logical laye. here we define query logic to whatever data can be access from the database.

Create Class BAL.CS :

public class BAL
{
SqlConnection con = new SqlConnection(@”Data Source=.\SQLEXPRESS;AttachDbFilen Instance=True“);

public DataTable select_data(DAL da)
{
DataTable dt = new DataTable();

SqlDataAdapter adp = new SqlDataAdapter(“select id,name,city from user_master“, con);
adp.Fill(dt);
return dt;
}
}

Write Code In WebForm2.aspx.cs :

public partial class WebForm2 : System.Web.UI.Page
{
DataTable dt = new DataTable();
DAL da = new DAL();
BAL ba = new BAL();

SqlConnection con = new SqlConnection(@”Data Source=.\SQLEXPRESSnstance=True“);

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{

grid();
}
}
public void grid()
{

dt = ba.select_data(da);
GridView1.DataSource = dt;
GridView1.DataBind();

}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
grid();
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
grid();

}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
delete(id);
grid();

}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
TextBox txt_name = (TextBox)GridView1.Rows[e.RowIndex].FindControl(“txt_name“);
TextBox txt_city = (TextBox)GridView1.Rows[e.RowIndex].FindControl(“txt_city“);
update(id,txt_name.Text,txt_city.Text);
GridView1.EditIndex = -1;
grid();
}
private void update(int id, string txt_name, string txt_city)
{
con.Open();
SqlCommand cmd = new SqlCommand(“update user_master set name=‘” + txt_name + “‘,
city='” + txt_city + “‘ where id=” + id + “”, con);
cmd.ExecuteNonQuery();
con.Close();

}

private void delete(int id)
{
DAL da = new DAL();
con.Open();
SqlCommand cmd = new SqlCommand(“delete from user_master where id='” + id + “‘”, con);
cmd.ExecuteNonQuery();
con.Close();

}

}
}

Now Run your project and see output

Insert, Update, Delete In GridView Using ASP.Net C#

SEE MORE

 

29 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *