locked
Deleting data from GridView RRS feed

  • Question

  • User-2055741253 posted

    Hello,

    I am running into an error when deleting data from the grid view. Here is the error I get: object reference not set to an instance of an object.

    Here is the code below:

    aspx page:

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"  ForeColor="#333333" GridLines="None" PageSize="20" 
                OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" ShowFooter="True"  >
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <Columns>
                    
                    <asp:TemplateField HeaderText="ID">
                        <ItemTemplate>
                            <asp:Label ID="lblID" runat="server" Text='<%# Bind("BRD_COMMENT_ID") %>'>
    
                            </asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label ID="txtID" runat="server" Text='<%# Bind("BRD_COMMENT_ID") %>'>
    
                            </asp:Label>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    
                    <asp:TemplateField HeaderText="Comments">
                        <ItemTemplate>
                            <asp:Label ID="lblComment" runat="server" Text='<%# Bind("COMMENT_TEXT") %>'>
    
                            </asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtComment"  Width="900px" runat="server" Text='<%# Bind("COMMENT_TEXT") %>'>
    
                            </asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID ="lnkEdit" runat="server" CommandName="Edit" Text ="Edit"></asp:LinkButton>
                            <asp:LinkButton ID ="lnkDelete" runat="server" CommandName="Delete" Text ="Delete"></asp:LinkButton>
                        </ItemTemplate>
                        <EditItemTemplate>   
                           <asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update"/>   
                           <asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel"/>   
                        </EditItemTemplate>
                    </asp:TemplateField>

    aspx.cs:

      protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
            {
                Label id = GridView1.Rows[e.RowIndex].FindControl("txtID") as Label;
                TextBox comment = GridView1.Rows[e.RowIndex].FindControl("txtComment") as TextBox;
                var deleteQuery = ("DELETE FROM CHNGMETRICS.VW_BRD_COMMENT WHERE BRD_COMMENT_ID = '" + Convert.ToInt32(id.Text) + "' ");
                OracleCommand command = new OracleCommand(deleteQuery);
                Utility.Exec(command);
                RefreshGrid();
            }

    Monday, July 10, 2017 7:14 PM

Answers

  • User-1716253493 posted

    Hi gssingh04,

    Delete button is in ItemTemplate, the button is available in readonly (not edit mode)

    so you should get the id from label in ItemTemplate also

      protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
            {
                Label id = GridView1.Rows[e.RowIndex].FindControl("lblID") as Label;
                //TextBox comment = GridView1.Rows[e.RowIndex].FindControl("txtComment") as TextBox;
                var deleteQuery = ("DELETE FROM CHNGMETRICS.VW_BRD_COMMENT WHERE BRD_COMMENT_ID = '" + Convert.ToInt32(id.Text) + "' ");
                OracleCommand command = new OracleCommand(deleteQuery);
                Utility.Exec(command);
                RefreshGrid();
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 11, 2017 1:03 AM