Last post Feb 22, 2015 01:04 AM by Archer Wang
Feb 17, 2015 10:15 AM|Martin91|LINK
My web application creates a number of cookies on a button click within a device list (generated by a listview), these cookies contain some data and are created successfully, using the below code
HttpCookie cookie = new HttpCookie(DeviceNo);
This means that each cookie created is given the name of the items Device number, so all cookies have a different name
The issue i am having is retrieving the cookies
The code used to retrieve the cookie data is;
if (Request.Cookies["cookie"] != null)
var cookie = Request.Cookies["cookie"].Value;
Quote.Text = Environment.NewLine + cookie;
This code returns no data, however if i change "Request.Cookies["cookie"] to "Request.Cookies["nameOfIndividualCookie"]" Then i am able to get the data from that individual cookie.
Is there any way to gather the data from all cookies which are created using "HttpCookie cookie = new HttpCookie(DeviceNo);" section of code?
Feb 17, 2015 10:39 AM|AidyF|LINK
You can use individual values inside a single cookie
use similar code to read them back
Feb 17, 2015 10:56 AM|mgebhard|LINK
IMHO, it's better to name cookies. Here's how to enumerate over the cookie collection. The code sample assumes you have a label1 control on the page to hold the results.
protected void PrintCookiesData()
StringBuilder sb = new StringBuilder();
HttpCookieCollection cookieCollection = Request.Cookies;
foreach (string key in cookieCollection.Keys)
sb.Append("Cookie retrieved from client. <br/>");
sb.Append("Cookie Name: " + cookieCollection[key].Name + "<br/>");
sb.Append("Cookie Value: " + cookieCollection[key].Value + "<br/>");
sb.Append("Cookie Expiration Date: " +
cookieCollection[key].Expires.ToString() + "<br/>");
Label1.Text = sb.ToString();
Feb 18, 2015 03:40 AM|Archer Wang|LINK
As mgebhard said, you'd better to use below code to get your data.
In the MSDN's library, it has shown you an example to refer.
For more information, I suggest that you could refer to below link.
Hope it's helpful to you.
Feb 18, 2015 05:38 AM|Martin91|LINK
Thanks for the replies, Im still having issues with this. Apologies if i am repeating here, each time a cookie is created it is given the name of the Device Number which is held in a database. This means there are hundreds of possible names which could be
given to the cookies (however it is highly unlikely a customer would create more than 10 cookies when using the site, the cookies are set to last 1 hour and would be expired when the user submits the quote).
If when the cookies are created, can they be added to a cookie collection, which can then be retrieved using the Request.Cookies method?
HttpCookie cookie = new HttpCookie(PartNo);
//add the cookie
Feb 22, 2015 01:04 AM|Archer Wang|LINK
As far as I know, you could use HttpRequest.Cookies Property. This property gets a collection of cookies sent by the client.
For more information, please refer to the below link.
Also, for the Request.Cookies Collection, I suggest that you could refer to the below link.