Solved

Why is this cookie  value always null?

Posted on 2008-06-18
6
1,309 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
ID: 21812929
the value wont be available until the page does a postback
0
 

Author Comment

by:dtburdick
ID: 21813166
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
ID: 21813280
you are never getting the cookie you need something like this
HttpCookie cookie1=  Response.Cookies.Get("preferences");
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:dtburdick
ID: 21814120
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
ID: 21814188
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
ID: 31468337
Thank you so much.  I'd have been here for eternity looking at that.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

813 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

9 Experts available now in Live!

Get 1:1 Help Now