Problem deleting row from datatable

chris_desborough
chris_desborough used Ask the Experts™
on
Experiencing a problem with deleting a row from a gridview bound to a datatable.

Get message: Object reference not set to an instance of an object on this line:

dtItems.Rows[e.RowIndex].Delete(); in the gvOrderSummary_RowDeleting method.
Gridview:
<asp:GridView ID="gvOrderSummary" runat="server" OnRowDeleting="gvOrderSummary_RowDeleting"
            OnRowDataBound="gvOrderSummary_RowDataBound" BackColor="White" BorderColor="#CCCCCC"
            BorderStyle="None" BorderWidth="1px" CellPadding="3" PageSize="100"  
            DataKeyNames="ID" AutoGenerateDeleteButton="True"  >
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="btnDelete" runat="server" Text="Del" CommandArgument='<%#DataBinder.Eval(Container,"DataItem.ID")%>'
                            CommandName="Delete"/>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <RowStyle ForeColor="#000066" />
            <FooterStyle BackColor="White" ForeColor="#000066" />
            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
            <EmptyDataTemplate>
                There were no items in your order. Click the &#39;Start a New Order&#39; to continue.
            </EmptyDataTemplate>
            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                   <HeaderStyle BackColor="#cccccc" HorizontalAlign="Left" ForeColor="Black" />
 
        </asp:GridView>
 
Code behind:
using System;
using System.Drawing;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.Text;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
 
public partial class bob : System.Web.UI.Page
{
    static DataTable dtItems = null;
 
       
 
    protected void Page_Load(object sender, EventArgs e)
    {
 
        gvOrderSummary.DataSource = (DataTable)Session["dtItems"];
 
        gvOrderSummary.DataBind();
 
    }
 
    protected void btnSubmitOrder_Click(object sender, EventArgs e)
    {
        string redirectURL = "Stores.aspx?UserName=" + Request.QueryString["UserName"];
        Response.Redirect(redirectURL);
 
    }
 
    protected void btnNewOrder_Click(object sender, EventArgs e)
    {
        string redirectURL = "Stores.aspx?UserName=" + Request.QueryString["UserName"];
        Response.Redirect(redirectURL);
 
    }
 
    protected void gvOrderSummary_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
 
        GridViewRow deletedItem = gvOrderSummary.Rows[e.RowIndex];
 
        lblDeleted.Text = lblDeleted.Text + "Item deleted - " + deletedItem.Cells[3].Text + "<br />";
        Server.HtmlEncode(lblDeleted.Text);
 
        dtItems.Rows[e.RowIndex].Delete();
 
        //dtItems.AcceptChanges();
 
        gvOrderSummary.DataSource = dtItems;
        gvOrderSummary.DataBind();
 
    }
 
    protected void gvOrderSummary_RowDataBound(object s, GridViewRowEventArgs e)
    {
        // Hide the header of the leftmost column.
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[1].Visible = false;
        }
 
        // Hide the leftmost column of the data row.
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Visible = false;
        }
    }
 
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
dtIIems never gets set in your Page_Load

Likewise I don't see where it's getting saved to the session either.

Author

Commented:
Okay, dtItems comes from a previous page - code here. Bit new to this stuff so bear with me. OrderSummary.aspx contains the Gridview code above.

 protected void lnkCheckOut_Click(object sender, EventArgs e)
    {
       
       Session["dtItems"] = dt;
       Response.Redirect("OrderSummary.aspx");
   
    }
Commented:
OK that answers the saving part of my question, but you're still not setting dtItems there.  In the page_load it should look more like this:

       dtItems = (DataTable)Session["dtItems"]; // added this line
        gvOrderSummary.DataSource = dtItems; // changed this line

        gvOrderSummary.DataBind();

Author

Commented:
Brilliant Mike. Works like a charm now.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial