对象引用未设置为对象的实例 - 尝试添加到会话时出错

时间:2014-05-18 08:13:47

标签: c# asp.net session

我目前正在将我的产品页面中保存的值添加到新的“订单”会话中,该会话由“OrderLines”组成。我实例化了一个新的orderLine,并使用该变量将页面中的值添加到orderLine值中,但是当我运行它并在gridview上点击“select”时,它会抛出上述错误。 (我的会话在Global.asax中声明)

这是抛出错误的代码:

protected void grdProducts_SelectedIndexChanged(object sender, EventArgs e)

        {
            //get the selected row

            string prodID = grdProducts.SelectedRow.Cells[1].Text;
            string name = grdProducts.SelectedRow.Cells[2].Text;
            //Accessing TemplateField Column controls
            string price = (grdProducts.SelectedRow.FindControl("lblPrice") as Label).Text;
            string colour = grdProducts.SelectedRow.Cells[4].Text;
            string size = grdProducts.SelectedRow.Cells[5].Text;
            string amount = (grdProducts.SelectedRow.FindControl("txtRequiredAmount") as TextBox).Text;
            string desc = grdProducts.SelectedRow.Cells[8].Text;

            //MessageLabel.Text += "<b>Name:</b> " + name + " <b>Price:</b> " + price + " <b>Colour:</b> " + colour + " <b>Size:</b> " + size + " <b>Quantity:</b> " + amount +
            //    " <b>Description:</b> " + desc;



            //get basket out of session

            Order aBasket = (Order)this.Session["Order"];
            OrderLine oneOLine = new OrderLine();

           **// Specifically the error occurs as soon as it hits here**

            oneOLine.Product.ProductID = Convert.ToInt32(prodID);
            oneOLine.Product.ProductName = name;
            oneOLine.Product.ProductPrice = Convert.ToDecimal(price);
            oneOLine.Product.ProductColour = colour;
            oneOLine.Product.ProductSize = size;
            oneOLine.ProductQuantity = Convert.ToInt32(amount);
            oneOLine.Product.ProductDesc = desc;

            aBasket.OrderLines.Add(oneOLine);

            //put it back in the session
            Session.Add("Order", aBasket);
        }

以下是宣布我的会话的代码:

void Session_Start(object sender, EventArgs e)

        {
            //######################## TEMP does not go here
            Order Basket = new Order();
            OrderLine oneOLine = new OrderLine();
            Session.Add("Order", Basket);
            //######################## TEMP
        }

这是ORDER的代码,它是我在创建EDMX时由VS自动生成的。

 namespace Practice
   {
  using System;
   using System.Collections.Generic;

public partial class Order
{
    public Order()
    {
        this.Invoices = new HashSet<Invoice>();
        this.OrderLines = new HashSet<OrderLine>();
        this.Deliveries = new HashSet<Delivery>();
    }

    public int OrderID { get; set; }
    public decimal OrderTotal { get; set; }
    public Nullable<System.DateTime> OrderDate { get; set; }
    public int StaffID { get; set; }
    public int CustomerID { get; set; }

    public virtual Customer Customer { get; set; }
    public virtual ICollection<Invoice> Invoices { get; set; }
    public virtual ICollection<OrderLine> OrderLines { get; set; }
    public virtual Staff Staff { get; set; }
    public virtual ICollection<Delivery> Deliveries { get; set; }
}

}

0 个答案:

没有答案