Solved

Why is this cookie  value always null?

Posted on 2008-06-18
6
1,331 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Winforms -- Capture CTRL+SHIFT+D at the form not working? 3 26
jquery + C#.net radiobuttonlist 3 30
linq, c# 8 24
asp.net mvc 2 25
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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