Skip to content
Loading
  • 0
    try the code snippet provided. hope it helps to fix the issue
    0
  • 0
    Now it is working, I have made these changes here:-
     
    In Gridview on .aspx file:
    1. <asp:GridView ID="grd" runat="server" AutoGenerateColumns="False" OnRowDataBound="grd_RowDataBound" Width="100%" CssClass="grid-view" OnRowDeleting="grd_RowDeleting" DataKeyNames="InsuranceId">    
    2.      
    3. <asp:TemplateField HeaderText="Delete">    
    4. <ItemTemplate>    
    5. <asp:ImageButton ID="btnDelete" runat="server" AlternateText="Delete" CommandName="Delete" CommandArgument='<%# Eval("InsuranceId") %>' ImageUrl="~/Images/buttondelete.png" CausesValidation="false" >asp:ImageButton>    
    6. ItemTemplate>    
    7.      
    8. Columns>    
    9. asp:GridView>    
    10. asp:TemplateField>  
    " imageurl="~/Images/buttondelete.png" causesvalidation="false">
     
    In ImageButton .aspx.cs file:
    1.    
    2. protected void grd_RowDeleting(object sender, GridViewDeleteEventArgs e)  
    3. {  
    4. bool IsDeleted = false;  
    5. try  
    6. {  
    7. //int InsuranceId = Convert.ToInt32(grd.DataKeys[e.RowIndex].Value.ToString());  
    8. int InsuranceId = Convert.ToInt32(grd.DataKeys[e.RowIndex].Values[0]);  
    9. string constr = WebConfigurationManager.ConnectionStrings["WarehouseWebsiteString"].ConnectionString;  
    10. using (SqlConnection con = new SqlConnection(constr))  
    11. {  
    12. using (SqlCommand cmd = new SqlCommand("DELETE FROM tblInsuranceCompany WHERE InsuranceId = @InsuranceId"))  
    13. {  
    14. cmd.Parameters.AddWithValue("@InsuranceId", InsuranceId);  
    15. cmd.Connection = con;  
    16. con.Open();  
    17. IsDeleted = cmd.ExecuteNonQuery() > 0;  
    18. con.Close();  
    19. }  
    20. fillgrid();  
    21. }  
    22. }  
    23. catch(Exception ex)  
    24. {}  
    25. if (IsDeleted)  
    26. {  
    27. //record has been deleted successfully!  
    28. //call here gridview bind method and replace it..  
    29. ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Page Succesfully deleted');window.location ='InsuranceCompany.aspx';", true); ;  
    30. fillgrid();  
    31. }  
    32. else  
    33. {  
    34. }
    0