0

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.

chobowski
  • 109
  • 1
  • 13
  • You are missing the event handler for RowCommand which will capture the clicking of linkbutton (`CommandArgument='<%#Bind("EMPNO")%>' CommandName="DeleteEmp"`) . Also, your code is vulnerable to SQL Injection, use sqlparameters instead of string concatenation, Refer [bobby-tables.com/dotnet.html](http://bobby-tables.com/dotnet.html) – Techie Dec 20 '16 at 08:52
  • This thread http://stackoverflow.com/questions/10783962/get-value-of-gridview-cell-in-rowcommand?rq=1 should help you to get going – Techie Dec 20 '16 at 08:54
  • here's my 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 – chobowski Dec 20 '16 at 08:55
  • Please update the question with all the relevant information. Also, where is the object reference exception pointing to? – Techie Dec 20 '16 at 08:56
  • I think it's not getting any value – chobowski Dec 20 '16 at 08:59
  • Why is the code to delete data in `btnYes_Click` instead of rowcommand procedure? Which line is giving the object reference error? For delete confirmation, are you using client side or server side code? Why does the ` – Techie Dec 20 '16 at 09:15
  • there's a dialog box that will appear and there's a btnYes_Click there so when I click yes the data will be deleted. – chobowski Dec 21 '16 at 00:18

0 Answers0