I hope you can help me with this one. I know this question is already asked too many times but the answer and the solution that I searched is not working for me.
I'm trying to delete a record in a database that is displayed in gridview.
I have an image button delete in gridview. when I click that the dialog box will appear asking if I really wanted to delete the record permanently and into the database.
Here's my code for asp
<asp:GridView ID="gvUsers" runat="server" AllowPaging="true"
AllowSorting="True" AutoGenerateColumns="False" BorderColor="Silver"
BorderWidth="1px" Width="100%">
<RowStyle Font-Names="Arial" Font-Size="9pt" HorizontalAlign="Center" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server"
CommandArgument='<%#Bind("EMPNO")%>' CommandName="EditEmp">
<asp:Image ID="imgEdit" runat="server" AlternateText="Edit Employee"
ImageUrl="eHR_Images/edit.png" />
</asp:LinkButton>
</ItemTemplate>
<HeaderStyle Width="60px" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField Visible="true">
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" runat="server"
CommandArgument='<%#Bind("EMPNO")%>' CommandName="DeleteEmp">
<asp:Image ID="imgDelete" runat="server" AlternateText="Delete Employee"
ImageUrl="eHR_Images/delete.gif" />
</asp:LinkButton>
</ItemTemplate>
<HeaderStyle Width="60px" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Empno" SortExpression="EMPNO">
<ItemTemplate>
<asp:Label ID="lblempno" runat="server" Text='<%#Bind("EMPNO")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" SortExpression="FIRSTNAME">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%#Bind("FIRSTNAME")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" SortExpression="LASTNAME">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%#Bind("LASTNAME")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Entity" SortExpression="ENTITY">
<ItemTemplate>
<asp:Label ID="lblEntity" runat="server" Text='<%#Bind("ENTITY")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location" SortExpression="LOCATION">
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%#Bind("LOCATION")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle Font-Size="9pt" HorizontalAlign="Right" />
<EmptyDataTemplate>
<div style="width: 100%; font-size: 10pt; text-align: center; color: Red;">
No record found.
</div>
</EmptyDataTemplate>
<HeaderStyle BackColor="DarkGray" Font-Bold="True" Font-Names="Arial"
Font-Size="9pt" ForeColor="White" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="Gainsboro" />
</asp:GridView>
and when I click the yes button the record will be deleted.
here's my code for btnYes_Click
Try
Dim firstname As String = DirectCast(gvUsers.SelectedRow.FindControl("lblFirstName"), Label).Text
Dim lastname As String = DirectCast(gvUsers.SelectedRow.FindControl("lblLastName"), Label).Text
Dim entity As String = DirectCast(gvUsers.SelectedRow.FindControl("lblEntity"), Label).Text
Dim location As String = DirectCast(gvUsers.SelectedRow.FindControl("lblLocation"), Label).Text
UpdateInsDelRecord("DELETE FROM emp_mastertbl WHERE empno='" & Me.lblRowVal.Text & "' AND FIRSTNAME = '" & firstname & "' AND LASTNAME = '" & lastname & "' AND ENTITY = '" & entity & "' AND LOCATION '" & location & "';" & _
"DELETE FROM empgroup_tbl WHERE SEQID ='" & Me.lblRowVal.Text & "';")
'MessageBox("alert('Employee successfully deleted.');window.location='eHR_EmpMaintenance.aspx';")
MessageBox("alert('Employee successfully deleted.');")
Response.Redirect("eHR_EmpMaintenance.aspx?def_ent2=" + Me.ddlEntity.SelectedValue.ToString() + "&def_loc2=" + Me.ddlLocation.SelectedValue.ToString())
Me.divDialog.Visible = False
Catch ex As Exception
MessageBox("alert('Error occured during deletion.');")
Throw
End Try
code for rowcommand
If e.CommandName = "EditEmp" Then
Response.Redirect("eHR_EditDetails.aspx?emp_id=" + e.CommandArgument + "&opt=opt_edit")
ElseIf e.CommandName = "DeleteEmp" Then
val_e = e.CommandArgument
Me.divDialog.Visible = True
Me.lblRowVal.Text = val_e
End If
I'm trying to delete the employee's record that is shown in the girdview list but there's an error
Object reference not set to an instance of an object.
Please help me with this. Thank you.