Cookie Help

Last post 08-19-2008 3:28 AM by Zong-Qing Li - MSFT. 3 replies.

Sort Posts:

  • Cookie Help

    08-15-2008, 1:48 AM
    • Member
      17 point Member
    • chekmate
    • Member since 02-14-2008, 5:41 PM
    • Posts 87

    My site was working a week ago, and now suddenly it isn't.

    I can't get it to read the cookies I am saving. Maybe you can help.

    This is how I am writing the cookie:

    Dim cookie As HttpCookie = Request.Cookies("EmployeeID")
    If Request.Cookies("EmployeeID") Is Nothing Then
    Response.Cookies("EmployeeID").Value = Request.QueryString("employeeid")
    Response.Cookies(
    "EmployeeID").Expires = DateTime.Now.AddDays(7)
    End If

    And now I can't get the site to read the cookie for use in datagrids etc.

  • Re: Cookie Help

    08-15-2008, 4:36 AM
    Answer
    • Participant
      1,051 point Participant
    • O11Y
    • Member since 04-11-2006, 1:48 PM
    • Bristol, UK
    • Posts 307

    I can't see anything immediatly wrong with what you're doing. I personally use the Cookies.Add method to register new cookies so you could try it instead:

    (Writing)

    Page.Response.Cookies.Add(new System.Web.HttpCookie("My Cookie","My Value"));

    (Reading)

    if (Page.Request.Cookies["My Cookie"] != null)
      myVariable =
    Page.Request.Cookies["My Cookie"].Value; // myVariable = "My Value" 

  • Re: Cookie Help

    08-15-2008, 3:21 PM
    • Member
      17 point Member
    • chekmate
    • Member since 02-14-2008, 5:41 PM
    • Posts 87

    Now I am able to see the cookie on the page I am writing it, but as soon as I redirect to the next page the cookie seems to disappear.

    For example,

    Response.Redirect("/Admin/Default.aspx?id=" & Request.Cookies("employeeid").Value)

    takes me to the page with the id set to a number, but once on that page I cannot read the value from the cookie itself

  • Re: Cookie Help

    08-19-2008, 3:28 AM
    Answer

    Hi,

    Based on my understandings, you create the cookies in the first page, and then transfer to another page named second page. In the second page you get the cookie's value. But if the second page redirect to the third page, you can't get the cookie's value in the third page. If I understood the issue, I suggest to run the following code which run well under my environment.

    First.aspx.cs

            Page.Response.Cookies.Add(New System.Web.HttpCookie("employeeid", "My Value"))
            Response.Redirect("Second.aspx")

    Second.aspx.cs

           Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim myVariable As String = Page.Request.Cookies("employeeid").Value
            Response.Write(myVariable)

          End Sub

          Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            Response.Redirect("Third.aspx?id=" & Request.Cookies("employeeid").Value)
          End Sub

    Third.aspx.cs

           Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            Dim readFcookie As String = Page.Request.Cookies("employeeid").Value 'read from cookie
            Response.Write("read from cookie " & readFcookie)
            Response.Write("<br/>")
            Dim readFget As String = Request.QueryString("id").ToString() 'read from cookie
            Response.Write("read from get " & readFget)

        End Sub

    If it still not work, please post some relative code for here. I understand that this may be a lot of information to ask for at one time. However, by collecting this information now, it will help us move more quickly toward a solution.

     If I misunderstood the issue, please inform of me.

    Sincerely,
    Zong-Qing Li
    Microsoft Online Community Support
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as
    Answer” if a marked post does not actually answer your question.
Page 1 of 1 (4 items)