Hey all,

I am making a website for a project which allows the user to "purchase" items. Currently, I have all the items displayed in a grid view which has selection enabled.

When you click this, it stores the cell values into sessions which I retreive on another page as a label (my basket, if you will). What I would like to know is how to go further than just the 1 item, so I click my "Continue Shopping" button and it goes back to my shopping page where I can select another item which will again redirect to basket page and add it along side first item?

I have it working with 1 item at the moment, but when I go back and click another it overwrites the initial session.

Here is the code from my shopping page:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["CoffeeID"] = GridView1.SelectedRow.Cells[0].Text;
            Session["CoffeeName"] = GridView1.SelectedRow.Cells[1].Text;
            Session["CoffeePrice"] = GridView1.SelectedRow.Cells[2].Text;
            Session["CoffeeStrength"] = GridView1.SelectedRow.Cells[3].Text;
            Session["Origin"] = GridView1.SelectedRow.Cells[4].Text;
            Session.Timeout = 20;
            Response.Redirect("MoreDetails.aspx");
        }

And this from the basket page:

protected void Page_Load(object sender, EventArgs e)
        {
            lblID.Text = Session["CoffeeID"].ToString();
            lblName.Text = Session["CoffeeName"].ToString();
            lblPrice.Text = Session["CoffeePrice"].ToString();
            lblStr.Text = Session["CoffeeStrength"].ToString();
            lblOrigin.Text = Session["Origin"].ToString();
        }

Dani AI

Generated

A concise, maintainable fix for the overwrite problem reported by is to stop using separate Session keys per field and instead store a single collection (for example, a typed List<CartItem>) in Session. ’s numbered-session idea will work but becomes brittle as items and fields grow. A collection makes it easy to append items, merge duplicates (increment quantity), and calculate totals on MoreDetails or Checkout pages.

A minimal CartItem model and add-to-cart pattern:

[Serializable]
public class CartItem
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }
}

// when an item is selected:
var cart = Session["Cart"] as List<CartItem> ?? new List<CartItem>();
var existing = cart.FirstOrDefault(i => i.Id == selectedId);
if (existing != null) existing.Quantity++;
else cart.Add(new CartItem { Id = selectedId, Name = selectedName, Price = selectedPrice, Quantity = 1 });
Session["Cart"] = cart;

Reading the cart and computing totals is straightforward:

var cart = Session["Cart"] as List<CartItem> ?? new List<CartItem>();
decimal total = cart.Sum(i => i.Price * i.Quantity);

Practical notes and cautions: the GridView checkbox approach is useful for bulk adds but still benefits from a single collection in Session. Mark CartItem serializable when using StateServer or SQLServer session modes. Keep Session payloads small — for persistent carts, multiple devices, or high traffic, persist cart rows to a database and store only a short cart ID in Session or a cookie. Always validate item IDs and prices server-side (do not trust client values), and be aware that InProc session is lost on app-pool recycle. This approach keeps logic clear, avoids fragile indexed session keys, and simplifies checkout math and display.

What you could do is have one session to store the amount of items in the basket and then have the item sessions numbered, possibly like this:
Session["CoffeeID" + number] = GridView1.SelectedRow.Cells[0].Text;
Session["CoffeeName" + number] = GridView1.SelectedRow.Cells[1].Text;
Session["CoffeePrice" + number] = GridView1.SelectedRow.Cells[2].Text;
Session["CoffeeStrength" + number] = GridView1.SelectedRow.Cells[3].Text;
Session["Origin" + number] = GridView1.SelectedRow.Cells[4].Text;

And then use a for-loop like this:

for(int i = 0; i < Session["items"]; i++)
{
//Do whatever
}

It's up to you :)

Would that work for displaying the other item also?

What it currently does is output the data onto another form (moredetails.aspx), so if I was to hit the continue shopping button and choose a different item, the previous item will still be there and the new item will be next to it.

Then if I press the Checkout button, it will go to another page (say, checkout.aspx) which will carry over the "basket" items and have a label displaying the total price (item1 price * quantity)+(item2 price * quantity) for example.

Or would it be easier to include a check box in my grid view and have a button that adds all ticked rows to a cart session, then another button that goes to checkout page?

Would that work for displaying the other item also?

What it currently does is output the data onto another form (moredetails.aspx), so if I was to hit the continue shopping button and choose a different item, the previous item will still be there and the new item will be next to it.

Then if I press the Checkout button, it will go to another page (say, checkout.aspx) which will carry over the "basket" items and have a label displaying the total price (item1 price * quantity)+(item2 price * quantity) for example.

Yes, that would work, since it would store all the items in different sessions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.