Reputation: 119
I added a Delete Command button to my GridView but whenever i chick the button i get an error which is:
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";
}
}
Upvotes: 0
Views: 1243
Reputation: 33163
Nothing wrong with your code but you forgot to handle the RowDeleting
event of your grid view.
You need to handle the gridview RowDeleting
method:
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:
protected void NameOfYourGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//bind your grid view here...
}
Upvotes: 1