Stuart
Stuart

Reputation: 1574

gridview databind with linq query errors

I am trying to populate a gridview with a list of objects via linq (I only want to show certain objects with a specific property).

I get the error...

Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: source

Source Error:

Line 1364: returnInvoices = (List)Session["Invoices"]; Line 1365: Line 1366: var partInvoices = from i in returnInvoices Line 1367: where i.PartNo == partNo Line 1368: select new Source File: c:\inetpub\wwwroot...\Form.aspx.cs Line: 1366

protected void DoInvoicePopUp(string partNo)
{
    List<Invoice> returnInvoices = (List<Invoice>)Session["Invoices"];

    var partInvoices = from i in returnInvoices
                       where i.PartNo == partNo
                       select new
                       {
                           i.InvoiceID,
                           i.InvoiceNo,
                           i.InvoiceLine,
                           i.InvoiceDate,
                           i.OrderNo,
                           i.OrderLine,
                           i.OrderRel,
                           i.OrderLineItem,
                           i.OrderLineQty,
                           i.CustomerPO,
                           i.Serialized
                       };

    GridView3.DataSource = partInvoices;
    GridView3.DataBind();

    pnlBlanket.Visible = pnlInvoiceSearch.Visible = true;
}

I'm not sure what I'm doing wrong. The List has 200+ objects in it, so how can it be null?

Upvotes: 0

Views: 527

Answers (2)

DarknessBeginsHere
DarknessBeginsHere

Reputation: 602

GridView3.DataSource = partInvoices.ToList()

Upvotes: 0

Coding Flow
Coding Flow

Reputation: 21881

Looks like Session["Invoices"] is null, if it should never be null you need to look at why this is hapening, if it is fine for it to be null just use the as keyword instead of casting e.g.

List<Invoice> returnInvoices = Session["Invoices"] as List<Invoice>;

Session["Invoices"] might still be null but it won't throw the null ref exception on the cast, it will just you will just see that returnInvoices is null, which you can then check for and handle.

EDIT

I willl leave the above as general advice but actually from the stack trace it looks like partNo is null.

Upvotes: 1

Related Questions