Solved

Why is this cookie  value always null?

Posted on 2008-06-18
6
1,277 Views
Last Modified: 2010-04-21
I've tried it a couple of ways and it always comes up as null on Page_Load().  The value is what I expect it to be right after I set it.


protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)

        {

 

            HttpCookie cookie = new HttpCookie("Preferences");

 

            if (cookie != null)

                ddl_report.SelectedValue = cookie["LastReportRun"];

            

            //The previous line is executed, but cookie["LastReportRun"] is always null.
 

            SetCriteriaVisibility();

        }

    }

 

 

    protected void btn_run_report_Click(object sender, EventArgs e)

    {

        HttpCookie cookie = new HttpCookie("Preferences");

        cookie.Values.Add("LastReportRun", ddl_report.SelectedValue);

        cookie.Expires = DateTime.Now.AddMonths(2);

        Response.Cookies.Add(cookie);

 

        string s = cookie["LastReportRun"];

    }

Open in new window

0
Comment
Question by:dtburdick
  • 3
  • 2
6 Comments
 
LVL 16

Expert Comment

by:burakiewicz
Comment Utility
the value wont be available until the page does a postback
0
 

Author Comment

by:dtburdick
Comment Utility
So when do I set it?  The following didn't work even though it hits the line of code where it sets the cookie.  It is still null the next time I start the application.


    protected void Page_Load(object sender, EventArgs e)

    {

        if (ddl_report.SelectedIndex >0)

        {

            HttpCookie cookie = new HttpCookie("Preferences");

            cookie.Values.Add("LastReportRun", ddl_report.SelectedValue);

            cookie.Expires = DateTime.Now.AddMonths(2);

            Response.Cookies.Add(cookie);
 

            string s = cookie["LastReportRun"];

        }
 

        if (!Page.IsPostBack)

        {
 

            HttpCookie cookie = new HttpCookie("Preferences");
 

            if (cookie != null)

                ddl_report.SelectedValue = cookie["LastReportRun"];

            

            SetCriteriaVisibility();
 

        }

    }

Open in new window

0
 
LVL 16

Expert Comment

by:burakiewicz
Comment Utility
you are never getting the cookie you need something like this
HttpCookie cookie1=  Response.Cookies.Get("preferences");
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:dtburdick
Comment Utility
That isn't working either.  I hate it when simple stuff slows me down.
0
 
LVL 33

Accepted Solution

by:
raterus earned 125 total points
Comment Utility
You need to get the cookie from the Request, not the Response.

        if (!Page.IsPostBack)
        {
 
            HttpCookie cookie = Request.Cookies["Preferences"];
 
            if (cookie != null)
                ddl_report.SelectedValue = cookie["LastReportRun"];
           
            //The previous line is executed, but cookie["LastReportRun"] is always null.
 
            SetCriteriaVisibility();
        }
0
 

Author Closing Comment

by:dtburdick
Comment Utility
Thank you so much.  I'd have been here for eternity looking at that.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now