Problem getting cookie from code-behind (worked in code-beside)

Discussion in 'ASP .Net' started by Alan Silver, Sep 15, 2005.

  1. Alan Silver

    Alan Silver Guest

    Hello,

    I am just converting one of my pages from code-beside to code-behind. I
    removed all the C# and put that in a separate file (Basket.cs) and added
    references for all the ASP controls on the page. I changed the Page
    references to "using" and added a class around the code.

    When I tried to run the page, I got an error when I tried to reference
    Request.Cookies. I remembered form a previous time that you have to use
    HttpContext.Current to get at the Request object, so I added that.
    Trouble is, it now gives me an error "Object reference not set to an
    instance of an object" on the line that tries to get the cookie value.

    I have shown a very simplified version of the .cs file below. Anyone any
    idea what the problem is? The DisplayBasket method is called when the
    page first loads, and it is the first line of this that causes the
    problem. I have snipped the rest for clarity.

    Thanks for any help.



    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using ... a few others

    public class Basket : Page {

    protected Literal litSiteName; // plus a whole load more

    void DisplayBasket() {
    int basketId = Convert.ToInt32(HttpContext.Current.Request.Cookies["BasketID"].Value);
    // do other stuff
    }
    }

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Sep 15, 2005
    #1
    1. Advertising

  2. Alan Silver

    Alan Silver Guest

    <snip>
    > int basketId =
    >Convert.ToInt32(HttpContext.Current.Request.Cookies["BasketID"].Value);

    </snip>

    I should mention that I know the cookie exists. I've been using it for a
    couple of days when the code was in the same file. The problem has only
    arisen now I've moved the code to a separate file.

    I know I should check to see if the cookie exists first, but that isn't
    the problem in this case.

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Sep 15, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maxwell2006
    Replies:
    3
    Views:
    4,614
    Steven Cheng[MSFT]
    Apr 21, 2006
  2. Claudio Grondi
    Replies:
    0
    Views:
    374
    Claudio Grondi
    Apr 26, 2005
  3. Radu
    Replies:
    8
    Views:
    335
  4. petro

    User control not seen on code beside (behind) page

    petro, Sep 22, 2006, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    124
    petro
    Sep 25, 2006
  5. Don

    getting worked: Java Applet

    Don, Sep 28, 2004, in forum: Javascript
    Replies:
    0
    Views:
    62
Loading...

Share This Page