Removing a dictionaryentry from a session index (ASP)
<p>I am brand new to asp, so I think the answer to this should be an easy but I cannot figure out what I am doing wrong. This is a very simple shopping cart exercise I'm doing to learn ASP. I create the session, add things to it, and then on the checkout page should be able to remove items from the cart one by one. If I click remove, the item is removed from the listbox, but not from the session. I know it persists in the session because if I click the link to the shopping page, the listbox on the shopping page will populate with the item I thought that I removed.</p>

<p>I've tried removing it two different ways, and I'm not sure why it isn't working. </p>

<p>note: The dictionary keys are the actual names of the products being used to populate the listboxes. The value entry is the productID# which I don't actually use right now. The listbox on the shopping page is being populated with data from a sqlsource. SelectedItem.Text = product name (whats shown) and SelectedValue = productID (not used).</p>

<p>Thanks so much for your help</p>

<pre><code>//Default (shopping) page
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Create a new collection for the session indexer
            ArrayList cart = (ArrayList)Session["scart"];

            //Need to initialize a new object in case it's null
            if (cart == null)
            {
                cart = new ArrayList();
                Session["scart"] = cart;
            }

            //Show the shopping car listbox if there are items in it and the 
            //user returned from the checkout page
            else pnlItems.Visible = true;
            foreach (DictionaryEntry item in cart)
            {
                lbItems.Items.Add((string)item.Key);
            }
        }
        //Show the shipping cart listbox containing the items the user just added
        if (IsPostBack) pnlItems.Visible = true;
    }
    private void add_to_cart(string item, int value)
    {
        //Method to add the selected item to the collection/session indexer
        ArrayList cart = (ArrayList)Session["scart"];
        cart.Add(new DictionaryEntry(item, value));
    }
    protected void btnAddItem_Click(object sender, EventArgs e)
    {
        //Method to send the selected item to the add_to_cart method
        string item = ddlProducts.SelectedItem.Text;
        lbItems.Items.Add(item);
        int value = int.Parse(ddlProducts.SelectedValue);
        add_to_cart(item, value);
    }
    protected void ddlProducts_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void btnCheckOut_Click(object sender, EventArgs e)
    {
        //Send the user to the checkout page when the button is clicked
        Response.Redirect("checkout.aspx", true);
    }

}

//CheckoutPage:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Populate the shopping cart on the checkout page with the items in the collection
            ArrayList cart = (ArrayList)Session["scart"];
            foreach (DictionaryEntry item in cart)
            {
                lbCheckOut.Items.Add((string)item.Key);
            }
        }
    }
    protected void btnKillOrder_Click(object sender, EventArgs e)
    {
        //Kills the order by ending the session, then returns the user to the shopping page.
        Session.Clear();
        Response.Redirect("Default.aspx", true);
    }
    protected void btnRemove_Click(object sender, EventArgs e)
    {
        //Removes the selected item from the shopping cart listbox and the session indexer
        ArrayList cart = (ArrayList)Session["scart"];
        cart.Remove(lbCheckOut.SelectedItem.Text);
        lbCheckOut.Items.Remove(lbCheckOut.SelectedItem.Text);
        Session["scart"] = cart;
        //Session.Remove(lbCheckOut.SelectedItem.Text);
    }
}
</code></pre>
