I added a Delete Command button to my GridView but whenever i chick the button i get an error which is:
- [HttpException (0x80004005): The GridView ‘GridView1’ fired event RowDeleting which wasn’t handled.]
- System.Web.UI.WebControls.GridView.OnRowDeleting(GridViewDeleteEventArgs e) +1463321
but i dont know whats the event that i should use and what to delete and edit, here is my code:
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="lkDelte" runat="server" OnClientClick="return confirm('Are you sure you want to delete?')"
CommandName="Delete" CommandArgument='<%# ((GridViewRow) Container).RowIndex %>'></asp:Button>
</ItemTemplate>
</asp:TemplateField>
protected void gdCourse_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
string con_str = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(con_str);
SqlCommand com = new SqlCommand("dbo.delete_documents", con);
com.CommandType = CommandType.StoredProcedure;
SqlParameter pDocument_ID = new SqlParameter("@document_id", row);
com.Parameters.Add(pDocument_ID);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
else
{
Label2.Text = "Unable to delete";
}
}
Nothing wrong with your code but you forgot to handle the
RowDeletingevent of your grid view.You need to handle the gridview
RowDeletingmethod:protected void gvLineItems_RowDeleting(object sender, GridViewDeleteEventArgs e)In this case you can just rebind in this method or leave the method empty but add it’s signature so the event can fire correctly.
Add this to your program: