locked
Cannot assign an object from session state to a local object name RRS feed

  • Question

  • User2023844170 posted

    As I hope this debug snapshot shows, I just executed the currentCart <==== from a session state object line (165)

    currentCart is defined (line 161) as type ShoppingCart Class

    I am casting the Session[cart] to an object of type Shopping cart

    and if you look in the watch window below, you can see that Sesson["cart'] is holding the data correctly, and is being cast correctly

    to match the ShoppingCart class definition, yet currentCart is still null??  It executes the statement, the right side is evaluating to the correct values

    yet the assignment is not happening.  And there is no compiler or execution error.  Help please.

    Sunday, November 23, 2014 7:41 PM

Answers

  • User2023844170 posted

    Again, I really do appreciate you helping me out here.

    You have my concept reversed in your mind.  You are creating a List (of ShoppingCar objects)  and then you create a ShoppingCar item and call it a list (which it is not a list, it would be a single item).

    What I am doing is creating a ShoppingCart object from a ShoppingCart Class.  That class happens to to store internally a List ( of objects type PurchasedItem). It will also store other info about the customer whose shopping cart this is.

    =============================

    The good news is, I figured it out.  I was thinking that my code:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                        // create a new List and stick it into session state, only the first time
                        ShoppingCart MyCart = new ShoppingCart();
                        Session["cart"] = MyCart;
                }

    Would only create the ShoppingCart object MyCart once, because of the  if (!IsPostBack)

    but after each item is purchased and added to the object's List, I was returning to a catalog page, so that when they bought the 2nd item, it WAS going back through my if (!IsPostBack) and creating a new session object and overwriting it each time.  The fix was this:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    if (Session["cart"] == null)
                    {
                        // create a new List and stick it into session state, only the first time
                        ShoppingCart MyCart = new ShoppingCart();
                        Session["cart"] = MyCart;
                    }
                }

    Again, thanks for offering to help.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 25, 2014 1:57 AM

All replies

  • User2023844170 posted

    I also tried the longer form, but that did not help:

     currentCart = (ShoppingCart) HttpContext.Current.Session["cart"];

    Sunday, November 23, 2014 7:48 PM
  • User-271186128 posted

    Hi Sir,

    As for this issue, I suppose the reason is that you add the Shopping cart to a list, then store the list in session. So, when you retrieve the session, you need to convert it to a list, then get the Shopping cart. Please refer to the following code.

            protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack)
                {
                    List<ShoppingCar> carlist = new List<ShoppingCar>();
                    ShoppingCar list = new ShoppingCar();
                    list.FirstName = "AAA";
                    list.LastName = "bbb";
                    carlist.Add(list);
                    Session["cart"] = carlist;
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                List<ShoppingCar> carlist;
                ShoppingCar list;
                if(Session["cart"]!=null)
                {
                    carlist = (List<ShoppingCar>)Session["cart"];
                    list = carlist[0];
                }
                else
                {
                    list = new ShoppingCar();
                    list.FirstName = "BBB";
                    list.LastName = "ccc";
                }
    
                Label1.Text = list.FirstName + "  " + list.LastName;
            }

    Best Regards,
    Dillion

    Monday, November 24, 2014 9:35 PM
  • User2023844170 posted

    Thank you for taking the time to consider my problem.  However, your assumptions is not valid.

    I could have just made a List, and kept that in Session state, but I specifically wanted the shopping cart to be a more complex object, containing a list of purchased items

    ( of type my  PurchasedItem which is a class I defined ) but also holding other non-list fields. I haven't added these other fields yet, but eventually the class would look like this:

      public class ShoppingCart
        {
            public List<PurchasedItem> MyCartList = new List<PurchasedItem>();
            public int CustomerID { get; set; }
            public int StreetNumber { get; set; }
            public string Street { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public int ZIP { get; set; }
    
            public void AddItem(PurchasedItem item)
            {
                MyCartList.Add(item);
            }
    

    As my first screen show shows, the object is in Session state, and inside the object the List is present, and the list has one item of type PurchasedItem. So all seems well,

    except for that one assignment which does nothing  (line 165).

    thanks again.

    Monday, November 24, 2014 10:20 PM
  • User-271186128 posted

    Hi Sir,

    If possible, will you please post your code about save value in session, so we could quickly help you to solve your problem.

    If I save the value in session without using list. The debug snapshot shows as below.

    witout using list

    If I save the value in session as indicated in my previous reply, The debug snapshot shows as below.

    Best Regards,
    Dillion

    Tuesday, November 25, 2014 12:59 AM
  • User2023844170 posted

    Again, I really do appreciate you helping me out here.

    You have my concept reversed in your mind.  You are creating a List (of ShoppingCar objects)  and then you create a ShoppingCar item and call it a list (which it is not a list, it would be a single item).

    What I am doing is creating a ShoppingCart object from a ShoppingCart Class.  That class happens to to store internally a List ( of objects type PurchasedItem). It will also store other info about the customer whose shopping cart this is.

    =============================

    The good news is, I figured it out.  I was thinking that my code:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                        // create a new List and stick it into session state, only the first time
                        ShoppingCart MyCart = new ShoppingCart();
                        Session["cart"] = MyCart;
                }

    Would only create the ShoppingCart object MyCart once, because of the  if (!IsPostBack)

    but after each item is purchased and added to the object's List, I was returning to a catalog page, so that when they bought the 2nd item, it WAS going back through my if (!IsPostBack) and creating a new session object and overwriting it each time.  The fix was this:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    if (Session["cart"] == null)
                    {
                        // create a new List and stick it into session state, only the first time
                        ShoppingCart MyCart = new ShoppingCart();
                        Session["cart"] = MyCart;
                    }
                }

    Again, thanks for offering to help.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 25, 2014 1:57 AM