Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Missing Cookies?

Status
Not open for further replies.

ThatRickGuy

Programmer
Oct 12, 2001
3,841
US
Hey Guys, I've been trying to store some user options in a cookie, and I'm having nada for luck.

in my global.asax I have the following:
Code:
  Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires when the session is started
    Response.Write("Session Start!")
    Response.Cookies("Options")("Branches") = "'Madison','Milwaukee','Geneva',"
    Response.Cookies("Options")("OrderField") = "First Name"
    Response.Cookies("Options")("Departments") = False
    Response.Cookies("Options").Expires = Now.AddDays(20)
  End Sub

When my page comes up, I see the "Session Start!" message correctly. And on the Page_Load of the page in question I have the following:

Code:
    Dim sBranches As String = Response.Cookies("Options")("Branches")
    Response.Write(sBranches)

It works fine on the first load, the value of the cookie is displayed, but any refresh/post back after that and the nothing is displayed.

And there is nothing in the C:\Documents And Settings\<user>\Cookies folder. (I cleared it before testing, and have been hitting refresh in explorer)

Am I missing something obvious?

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Rick, This should work for you.
Code:
Private Sub Global_AcquireRequestState(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.AcquireRequestState
        Response.Write("Session Starts!")
        Response.Cookies("Options")("Branches") = "'Madison','Milwaukee','Geneva',"
        Response.Cookies("Options")("OrderField") = "First Name"
        Response.Cookies("Options")("Departments") = False
        Response.Cookies("Options").Expires = Now.AddDays(20)

    End Sub

[COLOR=red yellow]Note the sub named Global_AcquireRequestState, If use Application_AcquireRequestState you will see "Session Starts! Session Starts (Twice) [/color]



What would you attempt to accomplish if you knew you would not fail?
 
Thanks Ecreations. Putting the code in AcquireRequestState correctly creates the cookies. But it does so every time the page is loaded. Even when I hit a check box on the Options.aspx page and it posts, when it loads it has "Session Start!" at the top. If I only assign the cookies if the cookie is nothing then it works correctly the first time (after deleting the cookie) but as soon as I try to alter the cookie from the Options.aspx, the cookie returns Nothing.

Cookies don't have scope do they? Other then only being accessable from within the domain they were created from? I just can't figure out why I can't create a cookie in Global.aspx Session_Start and access it from any of my other aspx pages.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I just can't figure out why I can't create a cookie in Global.aspx Session_Start and access it from any of my other aspx pages.
I'm not sure why either Rick. It should in theory be fairly simple. Here's an example I just tried, and it seems to work:

Global.asax
Code:
    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session is started

        ' Add a test cookie
        Dim testCookie As HttpCookie = New HttpCookie("UserDetails")
        testCookie.Values("Value1") = "This is value 1"
        testCookie.Values("Value2") = "This is value 2"
        testCookie.Expires = System.DateTime.Now.AddYears(10)
        Response.Cookies.Add(testCookie)

    End Sub

Any page:
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Dim cookieCols As New HttpCookieCollection
        cookieCols = Request.Cookies
        Dim str As String
        ' Read and add all cookies to the list box
        For Each str In cookieCols
            Response.Write("Cookie: " & str & "<br>")
            Response.Write("Value:" & Request.Cookies(str).Value & "<br>")
        Next
    End Sub
This seems to work fine, regardless if I postback to the page or not.


--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Okay, so here's my new Session_Start:
Code:
  Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires when the session is started
    Response.Write("Session Start!")
    Dim OptCookie As HttpCookie = New HttpCookie("Options")
    OptCookie.Values("Branches") = "'Madison','Milwaukee',"
    OptCookie.Values("OrderField") = "First Name"
    OptCookie.Values("Departments") = "False"
    OptCookie.Expires = System.DateTime.Now.AddYears(10)
    Response.Cookies.Add(OptCookie)
  End Sub

And here is the Options.aspx page_load:
Code:
    Dim sBranches As String = Response.Cookies("Options")("Branches")
    Response.Write("<BR/><BR/>Branches: " & sBranches)

The sub that handles the check boxes' CheckedChanged events is empty.

Works fine on the first load. If I close the browser, goto the page, and authenticate. The cookie gets created and the value of "Branches" is correctly displayed.

As soon as I hit refresh, or check/uncheck a check box, the page posts, the cookie disappears, and the page_load spits out an empty string for the cookie value. Atleast now I can see the cookie file get created (and deleted at the postback?!?).

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Hmmm that is strange. What do you have in the sessionState node of your web.config file?

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Code:
    <sessionState 
            mode="InProc"
            stateConnectionString="tcpip=127.0.0.1:42424"
            sqlConnectionString="data source=127.0.0.1;user id=sa;password="
            cookieless="false" 
            timeout="20" 
    />

First time I bring the page up the trace shows:
Code:
Cookies Collection 
Name               Value                               Size 
ASP.NET_SessionId  5jrapqycal1yvev50psid4u2            42 
Options            (Branches='Madison','Milwaukee',)   39

2nd time, I see:
Code:
Name               Value                               Size 
ASP.NET_SessionId  5jrapqycal1yvev50psid4u2            42 
Options            (Branches='Madison','Milwaukee',)   39 
Options            (Branches=)                         17

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Ahh, I should have spotted this sooner! It should be:
Code:
        Dim sBranches As String = [b]Request[/b].Cookies("Options")("Branches")
        Response.Write("<BR/><BR/>Branches: " & sBranches)

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Me too!

I tried your code and saw that it disappeared then tried mine and it worked so I knew something funny was up!


--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Rick,
I am begining to I was drunk When I answered your question. I was completely Off.
It was 3:19 AM and I was thinking about Presisting Querystrings across an application.

Sorry for the Confusion Rick, and I am glad ca8msm got it for you.

Good luck


What would you attempt to accomplish if you knew you would not fail?
 
I've learned to avoid drunken coding. After the boolean variables naming standard was changed to phrases...

Nothing like reviewing code and seeing:
Code:
While CowsNotHome
   ...
wend

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top