0

I know it's probably something simple, but I just can't figure it out. Note that I'm doing this on my own PC, not through a server (localhost) and I've considered that might be the issue, but I see nothing online about it being the case so maybe it's just a thought.

So I am trying to simply get a string and store it into a cookie and then read it later. Here's the lines of code that "saves" the cookie and its information:

HttpCookie cookie = new HttpCookie("userName", someInfo);
Response.Cookies.Add(cookie);

lblProof.Text = "Value: " + Request.Cookies["userName"].Value;

If I try this method, it fails. No information is shown on the lblProof. At first, I thought maybe someInfo didn't have anything in it (note it's a string). However, when I set the lblProof.Text to someInfo, it DOES show it. I've tried simply doing

Response.Cookies["userName"].Value = someInfo;

But that didn't work either. So what's causing this thing to not work at all? And yes, I've tried HttpContext.Current.Response and Request.

K Seven
  • 21
  • 1
  • 4
  • please see http://stackoverflow.com/questions/3140341/how-to-create-persistent-cookies-in-asp-net – Emran Sadeghi Aug 24 '14 at 06:34
  • For cookie is important that cookie be accepted by Web browser. Just check your browser security part and see if cookie is enabled. – Nick Mehrdad Babaki Aug 24 '14 at 06:38
  • 2
    Kind of a longshot here, but are you literally attempting to run this in a single method? I assume not and your code example is just for brevity, but if that's an assumption on my part and you are running the first 3 lines above in a single method, my thinking here is that while you are adding the cookie value to the Response object, it's not going to be in the Request object until another subsequent call from the browser. I doubt this is what you are actually doing, but just in case. – Daniel Graham Aug 24 '14 at 07:28
  • Daniel, it was indeed just an example lol. Emran had the answer. Everywhere I looked online kept saying to use .Value, not .Values. The link he posted made it work correctly. Now it simply makes me question the differences between the two, but a little research will probably help me figure that out. Thanks a lot Emran. – K Seven Aug 24 '14 at 21:28

0 Answers0