Ddelete in Gridview without using AutoGenerateDeleteButton
how to delete in Gridview row without using AutoGenerateDeleteButton?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sreekanth ReddyPosted Jan 31, 2017, 10:44 PM
namespace WebApplication1
{
public partial class WebForm2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("server=.;database=abc;trusted_connection=true");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
public void bind()
{
SqlCommand cmd = new SqlCommand("select * from employee", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "con");
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "comdel")
{
int l = Convert.ToInt32(e.CommandArgument);
con.Open();
SqlCommand cmd = new SqlCommand("delete from employee where eno=@a", con);
cmd.Parameters.AddWithValue("@a", l);
int i = cmd.ExecuteNonQuery();
if (i == 1)
{
Response.Write("Record deleted");
bind();
}
else
{
Response.Write("Recodrd not deleted...");
}
Page_Load(sender, e);
}
}
}
}