Click here to Skip to main content
15,908,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have gridview in which i am fetching records from database, i have two link buttons columns at the right side of my gridview columns. one is for view and second is for delete...i want when user click on the delete button then the corresponding record is delete.
ASP.NET
<asp:GridView ID="gv_ads" runat="server" AutoGenerateColumns="False" 
                    onrowcommand="gv_ads_RowCommand" AllowPaging="True" 
                    OnPageIndexChanging="gv_ads_PageIndexChanging" CellPadding="4" 
                    ForeColor="#333333" GridLines="None" Width="998px" >
                    <AlternatingRowStyle BackColor="White" />
                   <Columns>
                           <asp:BoundField DataField="register_On" HeaderText="Register On" />
                           <asp:BoundField DataField="ad_id" HeaderText="Ad ID" />
                           <asp:BoundField DataField="category_name" HeaderText="Category" >
                           </asp:BoundField>
                           <asp:BoundField DataField="subcategory_name" HeaderText="Sub-Category" >
                           </asp:BoundField>
                           <asp:BoundField DataField="title" HeaderText="Ad Title" >
                           </asp:BoundField>
                           <asp:BoundField DataField="s_name" HeaderText="State" >
                           </asp:BoundField>
                           <asp:BoundField DataField="pincode" HeaderText="Pincode" />
                           
                           <asp:TemplateField HeaderText="View Ad">
                            <ItemTemplate>
                            <asp:LinkButton runat="server" ID="lnkView" CommandArgument='<%#Eval("ad_id") %>' CommandName="VIEW">View/Edit</asp:LinkButton>
                           </ItemTemplate>
                           </asp:TemplateField>

                           <asp:TemplateField HeaderText="Delete Ad">
                           <ItemTemplate>
                             <asp:LinkButton runat="server" ID="linkDelete" OnClientClick="return confirm('Are you sure you want to delete this entry?');" CommandArgument='<%#Eval("ad_id") %>' CommandName="DELETE">Delete</asp:LinkButton>
                           </ItemTemplate>
                           </asp:TemplateField>
                          
                   </Columns>
                    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                    <SortedAscendingCellStyle BackColor="#FDF5AC" />
                    <SortedAscendingHeaderStyle BackColor="#4D0000" />
                    <SortedDescendingCellStyle BackColor="#FCF6C0" />
                    <SortedDescendingHeaderStyle BackColor="#820000" />
                </asp:GridView>

C#
protected void gv_ads_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "VIEW")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            Response.Redirect("Ads_Details.aspx?ad_id=" + index);

        }
        else if (e.CommandName == "DELETE")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            SqlCommand cmd = new SqlCommand("sps_deletead", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@useremail", ses);
            cmd.Parameters.AddWithValue("@ad_id", index);
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                message = "Ad Deleted Successfully.";
                ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
                con.Close();
                BindGridview();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }

when i click on delete button the record is deleted from database but error comes "The GridView 'gv_ads' fired event RowDeleting which wasn't handled."
My code is working fine but Why this error is coming.
Posted
Comments
[no name] 17-May-14 13:33pm    
http://forums.asp.net/t/984999.aspx?fired+event+RowDeleting+which+wasn+t+handled
[no name] 17-May-14 13:34pm    
"My code is working fine but Why this error is coming".....come to the point pls.
Raj Negi 17-May-14 14:41pm    
i mean...i want to delete record by click the button, and it is deleting my record but after deleting it gives error.

change command "DELETE" to some other name like "AdsDelete" and change your condition as well
 else if (e.CommandName == "AdsDelete")
{

Or you can add the event OnRowDeleting="gv_ads_RowDeleting" to your grid like below

ASP
<asp:gridview id="gv_ads" runat="server" onrowdeleting="gv_ads_RowDeleting" xmlns:asp="#unknown"></asp:gridview>

generate the event code or write the event as below
C#
public void gv_ads_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{

} 
 
Share this answer
 
Comments
Raj Negi 17-May-14 14:45pm    
first one is works.
[no name] 17-May-14 15:13pm    
eh my 5
DamithSL 18-May-14 6:16am    
thank you!
Just Fire this Event in your Gridview.


C#
<asp:gridview id="GridView1" runat="server" xmlns:asp="#unknown">OnRowDeleting="GridView1_RowDeleting">
     </asp:gridview>


C#
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
       {
//leave it empty

       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900