Weird cookie problem

T

Todd

I'm writing a cookie in one asp.net page, then trying to read that new
value in another.

Problem is, the saved value disappears when the cookie code calls it!

Here's the code...

SETTING THE COOKIE
*********************************************************
Dim i As String = Request.QueryString("passedVal").ToString
Dim aCookie As HttpCookie = Request.Cookies("MyCookie")
aCookie.Values("value1") = aCookie.Values("value1") + 1
aCookie.Values("value2") = aCookie.Values("value2").ToString & i & ";"
Response.Redirect("page2.aspx")


READING THE COOKIE
*************************************************************
Dim aCookie As HttpCookie = Request.Cookies("MyCourses")
Dim MyVal1 As Integer = aCookie.Values("value1")
Dim MyVal2 as String = aCookie.Values("value2")


No matter what I do, MyVal2 is always a zero-length string (""). I am
expecting somthing like "1;2;3;4;" depending upon user input. The
values should get tacked on the end.

But it just aint workin!!!!

I've tested this on several machine, different browsers the result is
the same. Values set in page1 are gone when page2 trys to read it.

HELP!!!
 
L

Laurent Bugnion

Hi,
I'm writing a cookie in one asp.net page, then trying to read that new
value in another.

Problem is, the saved value disappears when the cookie code calls it!

Here's the code...

SETTING THE COOKIE
*********************************************************
Dim i As String = Request.QueryString("passedVal").ToString
Dim aCookie As HttpCookie = Request.Cookies("MyCookie")
aCookie.Values("value1") = aCookie.Values("value1") + 1
aCookie.Values("value2") = aCookie.Values("value2").ToString & i & ";"
Response.Redirect("page2.aspx")

You are getting an array from the request, and then modify the array's
values. However, if you want to pass the cookies to the browser (where
they will be stored), you must write them using Response.Cookies.

HTH,
Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top