Home
Manage Your Code
Snippet: Get a string of Shopping cart cookie data (C#)
Title: Get a string of Shopping cart cookie data Language: C#
Description: Returns a string with all the cookies cocatenated together. This can be passed to a business tier to retrieve a table of all shopping cart items. Views: 167
Author: David Ashworth Date Added: 10/7/2007
Copy Code  
1protected string GetCartString()
2    {
3        HttpCookieCollection hc = new HttpCookieCollection();
4        hc = Request.Cookies;
5        string ItemsQuantity = "";
6        foreach (String sc in hc)
7        {
8            HttpCookie c = hc[sc];
9            ItemsQuantity += c.Value.ToString().Trim() + ",";
10        }
11
12        //trim end comma
13        if (ItemsQuantity.Length > 0)
14            ItemsQuantity = ItemsQuantity.Substring(0, ItemsQuantity.Length - 1);
15
16        return ItemsQuantity;
17    }
Usage
The string is in the following format:  "ProductID:Quantity,ProductID:Quantity"