asp.net how to iterate over Session objects?

asp.net , Session , loops , foreach Poland
  • 11 years ago

    Hello guys,

    I have following questions. I am trying to write a simple e-commerce site and I use some code of my own. Basically it is a shopping cart. It uses Session to store some data, here comes some code:

    /** * The ShoppingCart class * * Holds the items that are in the cart and provides methods for their manipulation */ public class ShoppingCart { #region Properties

    public List<CartItem> Items { get; private set; }
    
    #endregion
    
    #region Singleton Implementation
    
    // Readonly properties can only be set in initialization or in a constructor
    public static readonly ShoppingCart Instance;
    // The static constructor is called as soon as the class is loaded into memory
    static ShoppingCart() {
        // If the cart is not in the session, create one and put it there
        // Otherwise, get it from the session
        if (HttpContext.Current.Session["ASPNETShoppingCart"] == null) {
            Instance = new ShoppingCart();
            Instance.Items = new List<CartItem>();
            HttpContext.Current.Session["ASPNETShoppingCart"] = Instance;
        } else {
            Instance = (ShoppingCart)HttpContext.Current.Session["ASPNETShoppingCart"];
        }
    }
    
    // A protected constructor ensures that an object can't be created from outside
    protected ShoppingCart() { }
    
    #endregion
    
    #region Item Modification Methods
    /**
     * AddItem() - Adds an item to the shopping
     */
    public void AddItem(int productId) {
        // Create a new item to add to the cart
        CartItem newItem = new CartItem(productId);
    
        // If this item already exists in our list of items, increase the quantity
        // Otherwise, add the new item to the list
        if (Items.Contains(newItem)) {
            foreach (CartItem item in Items) {
                if (item.Equals(newItem)) {
                    item.Quantity++;
                    return;
                }
            }
        } else {
            newItem.Quantity = 1;
            Items.Add(newItem);
        }
    }
    

    Now, I need to get some values from variables from this Session to send them to paypal. I can write some loop in Shoppinc cart to iterate over these objects:

    foreach (CartItem item in Items)

    The problem is that I need similar loop on a different page, Cart.aspx.cs, and foreach (CartItem item in Items) doesn't work any more.

    How can I access these variables from Cart.aspx.cs?

    Sorry for all this code. I would really appreciate some help.

    All the best,

    Pikej

  • 11 years ago

    in your cart.aspx.cs..

    Get from the HttpContext. There you have a session variable where you can retrieve from HttpContext.Current.Session["SessionName"] ! = null, just Get to List. I think this wil work.

    please send your question more clearly.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems.” - Jamie Zawinski