Saturday, March 24, 2012

Trouble with a simple cookie

Hey guys - hoping you can help me out here. I'm using asp.net 2 (VB) and
trying to set and retrieve values from a cookie. I've done this a few times
before w/o a problem but I seem to be overlooking something here. I'm even
following my previous working examples and I can't see the different.
I have a page that will remember the user's settings using a cookie. Ther
Calendar Events, recent orders, etc will be displayed or hidden based upon
the checkboxes they select (but I haven't gotten that far yet). I'm getting
an error at:
Object reference not set to an instance of an object.
strMyEvents = objCookie.Values("MyEvents")
After using the code below, I tried to search the PC for a cookie and
couldn't find it which makes me think it doesn't exist. Yet, when I
response.redirect the value of the cookie just to test, it indeed finds
"True"
Any ideas'
Here's a simplified version of my code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Page.IsPostBack = False Then
Dim objCookie As HttpCookie
Dim strMyEvents As String
objCookie = Page.Request.Cookies("Intranet")
If IsNothing(objCookie) = True Then
SetCookie("True")
End If
strMyEvents = objCookie.Values("MyEvents")
lblTest.Text = strMyEvents
End If
End Sub
Protected Sub SetCookie(ByVal strMyEvents As String)
Dim objCookie As HttpCookie
objCookie = New HttpCookie("Intranet")
objCookie.Expires = DateTime.MaxValue
objCookie.Values("MyEvents") = "True"
'Response.Redirect(objCookie.Values("MyEvents"))
End SubThe cookie should show up under the domain name you are hitting in your
temporary internet files. You have to refresh the folder after hitting the
page to see the cookie (a bit strange compared to other folders, but many of
the profile folders are not standard windows folders).
You cannot access the cookies until you redirect as the information is not
sent to the server until it is requesting another page. As it works, you are
definitely getting the cookie through.
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com
****************************************
*********
Think outside the box!
****************************************
*********
"Groove" <noway@.noemail.com> wrote in message
news:%23xgCynXDHHA.4396@.TK2MSFTNGP02.phx.gbl...
> Hey guys - hoping you can help me out here. I'm using asp.net 2 (VB) and
> trying to set and retrieve values from a cookie. I've done this a few
> times before w/o a problem but I seem to be overlooking something here.
> I'm even following my previous working examples and I can't see the
> different.
> I have a page that will remember the user's settings using a cookie. Ther
> Calendar Events, recent orders, etc will be displayed or hidden based upon
> the checkboxes they select (but I haven't gotten that far yet). I'm
> getting an error at:
> Object reference not set to an instance of an object.
> strMyEvents = objCookie.Values("MyEvents")
> After using the code below, I tried to search the PC for a cookie and
> couldn't find it which makes me think it doesn't exist. Yet, when I
> response.redirect the value of the cookie just to test, it indeed finds
> "True"
> Any ideas'
> Here's a simplified version of my code:
>
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> System.EventArgs)
> If Page.IsPostBack = False Then
> Dim objCookie As HttpCookie
> Dim strMyEvents As String
> objCookie = Page.Request.Cookies("Intranet")
>
> If IsNothing(objCookie) = True Then
> SetCookie("True")
> End If
>
> strMyEvents = objCookie.Values("MyEvents")
> lblTest.Text = strMyEvents
>
> End If
> End Sub
>
>
> Protected Sub SetCookie(ByVal strMyEvents As String)
> Dim objCookie As HttpCookie
> objCookie = New HttpCookie("Intranet")
> objCookie.Expires = DateTime.MaxValue
> objCookie.Values("MyEvents") = "True"
> 'Response.Redirect(objCookie.Values("MyEvents"))
>
> End Sub
>
Thanks for your help. I've triple checked everything...and still, no
cookie. Can you think of anything that might prevent cookies from being
set? I've never had problems with other sites.
WinXP Pro, IE7.
"Cowboy (Gregory A. Beamer)" <NoSpamMgbworld@.comcast.netNoSpamM> wrote in
message news:ufUwJXYDHHA.4404@.TK2MSFTNGP03.phx.gbl...
> The cookie should show up under the domain name you are hitting in your
> temporary internet files. You have to refresh the folder after hitting the
> page to see the cookie (a bit strange compared to other folders, but many
> of the profile folders are not standard windows folders).
> You cannot access the cookies until you redirect as the information is not
> sent to the server until it is requesting another page. As it works, you
> are definitely getting the cookie through.
> --
> Gregory A. Beamer
> MVP; MCP: +I, SE, SD, DBA
> http://gregorybeamer.spaces.live.com
> ****************************************
*********
> Think outside the box!
> ****************************************
*********
> "Groove" <noway@.noemail.com> wrote in message
> news:%23xgCynXDHHA.4396@.TK2MSFTNGP02.phx.gbl...
>
As far as i know there is a limit on the numebr of cookies that can be
set. i thinks its 20. I had this problem a while back. You may need to
do the response.cookies("MainCookie")("Subcookie") to set the values.
I hope this helps
Groove wrote:
> Thanks for your help. I've triple checked everything...and still, no
> cookie. Can you think of anything that might prevent cookies from being
> set? I've never had problems with other sites.
> WinXP Pro, IE7.
>
> --
>
>
>
> "Cowboy (Gregory A. Beamer)" <NoSpamMgbworld@.comcast.netNoSpamM> wrote in
> message news:ufUwJXYDHHA.4404@.TK2MSFTNGP03.phx.gbl...

0 comments:

Post a Comment