how to delete data from database using gridview in asp.net

Apr 19 2018 3:25 AM
This is my code in .aspx file i write:
 
<asp:GridView ID="grd" runat="server" AutoGenerateColumns="false" OnRowDataBound="grd_RowDataBound" Width="100%" CssClass="grid-view">
<Columns>
<asp:TemplateField HeaderText="Company Name">
<ItemTemplate>
<asp:Label ID="lblCompanyName" runat="server" Text='<%# Eval("CompanyName") %>'></asp:Label>
<%-- <asp:Label ID="Label1" runat="server" Text='<%# Eval("FromDate","{0:dd-MMM-yyyy}") %>'></asp:Label>&nbsp;&nbsp;&nbsp;
<asp:Label ID="Label2" runat="server" Text='<%# Eval("ToDate","{0:dd-MMM-yyyy}") %>'></asp:Label>--%>
</ItemTemplate>
<EditItemTemplate>
<table class="FormattedTable" >
<tr>
<td>Company Name:</td>
<td><asp:TextBox ID="txtCompanyName" runat="server" Text='<%# Eval("CompanyName") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvCompanyName" runat="server" ControlToValidate="txtCompanyName" SetFocusOnError="true" ErrorMessage="*" ValidationGroup="Insurance"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Burglary Policy Number :</td>
<td><asp:TextBox ID="txtBurglaryPolicyNumber" runat="server" Text='<%# Eval("BurglaryPolicyNumber") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPolicyNumber" runat="server" ControlToValidate="txtBurglaryPolicyNumber" SetFocusOnError="true" ErrorMessage="*" ValidationGroup="Insurance"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Burglary Cover Amount:</td>
<td><asp:TextBox ID="txtBurglaryCoverAmount" runat="server" Text='<%# Eval("BurglaryCoverAmount") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvCoverAmount" runat="server" ControlToValidate="txtBurglaryCoverAmount" SetFocusOnError="true" ErrorMessage="*" ValidationGroup="Insurance"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Fire Policy Number :</td>
<td><asp:TextBox ID="txtFirePolicyNumber" runat="server" Text='<%# Eval("FirePolicyNumber") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtFirePolicyNumber" SetFocusOnError="true" ErrorMessage="*" ValidationGroup="Insurance"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Fire Cover Amount:</td>
<td><asp:TextBox ID="txtFireCoverAmount" runat="server" Text='<%# Eval("FireCoverAmount") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtBurglaryCoverAmount" SetFocusOnError="true" ErrorMessage="*" ValidationGroup="Insurance"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>From Date :</td>
<td><asp:TextBox ID="txtFromDate" runat="server" Text='<%# Eval("FromDate") %>'></asp:TextBox>
<cc1:CalendarExtender ID="CalDate" runat="server" TargetControlID="txtFromDate" Format="MM/dd/yyyy"></cc1:CalendarExtender>
<asp:RequiredFieldValidator ID="rfvFromDate" runat="server" ControlToValidate="txtFromDate" SetFocusOnError="true" ErrorMessage="*" ValidationGroup="Insurance"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>To Date :</td>
<td><asp:TextBox ID="txtToDate" runat="server" Text='<%# Eval("ToDate") %>'></asp:TextBox>
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtToDate" Format="MM/dd/yyyy"></cc1:CalendarExtender>
<asp:RequiredFieldValidator ID="rfvTodate" runat="server" ControlToValidate="txtToDate" SetFocusOnError="true" ErrorMessage="*" ValidationGroup="Insurance"></asp:RequiredFieldValidator>
</td>
</tr>
</table>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="View/Update">
<ItemTemplate>
<asp:ImageButton ID="imgBtnshow" runat="server" CausesValidation="false"
CommandArgument='<%# Eval("InsuranceId") %>' ImageUrl="~/Images/buttonedit.png" onclick="imgBtnshow_Click" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="imgBtnEdit" runat="server" CommandArgument='<%# Eval("InsuranceId") %>' ImageUrl="~/Images/buttonsave.png" OnClick="imgBtnEdit_Click" ValidationGroup="WarehouseUpdate" />
<asp:ImageButton ID="imgBtnCancel" runat="server"
ImageUrl="~/Images/1350368827_save_delete.png" onclick="imgBtnCancel_Click"/>
</EditItemTemplate>
<HeaderStyle Width="50px" />
<ItemStyle Width="50px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:ImageButton ID="imgBtnDelete" runat="server" CommandArgument='<%# Eval("InsuranceId") %>' ImageUrl="~/Images/buttondelete.png" OnClick="imgBtnDelete_Click" OnClientClick="return confirm('Are you sure to delete this Record.')"/>
</ItemTemplate>
<%--ValidationGroup="Insurance"--%>
<HeaderStyle Width="50px" Horizontal VerticalAlign="Middle" />
<ItemStyle Width="50px" HorizontalAlign="Center" VerticalAlign="Top" />
</asp:TemplateField>
</Columns>
</asp:GridView>
 
 
And i write this code in .aspx.cs file on imgBtnDelete_Click event:
 
 
protected void imgBtnDelete_Click(object sender, ImageClickEventArgs e)
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
ImageButton btn = (ImageButton)sender;
Int64 Id = Convert.ToInt64(btn.CommandArgument);
InsuranceCompany obj = new InsuranceCompany();
obj.InsuranceCompanyId = Id;
int result = obj.InsuranceCompanyDelete(Id);
if (result > 0)
{
string popupScript = "$.prompt('Record Deleted Successfully.');";
ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "clientScript", popupScript, true);
fillgrid();
//Response.Redirect("~/Admin/InsuranceCompany.aspx");
}
}
}
 

Answers (5)