user520351
user520351

Reputation:

Multiple entries through a session variable in ASP.NET

I'm creating a shopping basket in ASP.NET using session variables to pass the data from a shopping.aspx page to basket.aspx, currently I have the pages passing the primary key of the product with a gridview on the basket.aspx used to display the data from the database.

However this only works for one item at a time, how can I extended the session variable so multiple products can be added, as well as quantities etc?

Upvotes: 0

Views: 1126

Answers (2)

Hans Kesting
Hans Kesting

Reputation: 39274

You can put (almost) any object into the session, not just strings. So you could use a List<string> for a list of keys, or even a List<Product>.

EDIT
So in the first page you would get

var bookids = new List<string>();
// collect all book IDs into the 'bookids' list
Session["bookIDs"] = bookids;

and in the second page:

var bookids = Session["bookIDs"] as List<string>;
// use all IDs

Upvotes: 2

dariol
dariol

Reputation: 1979

You can use your own object, eg. Basket which can have one or more properties.

Object should be market as Serializable.

For example:

[Serializable]
public class Basket
{
    public List<BasketItem> Items {get;set;}
    public int UserId {get;set;}
}

[Serializable]
public class BasketItem
{
    //...
}

Upvotes: 2

Related Questions