Error when using Request.Cookies

Discussion in 'ASP.NET / ASP.NET Core' started by blueprintpm, Aug 30, 2007.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. Thanks in advance for any help. Do I have to set something special up to use request.cookies? I get thiserror if I use either of the statements for request.cookie or response.cookie(see the end of this question):

    Server Error in '/' Application.
    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:





    Code:
    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
    Stack Trace:





    Code:
    [NullReferenceException: Object reference not set to an instance of an object.]
       TelecomTracking.login.Page_Load(Object sender, EventArgs e)
       System.Web.UI.Control.OnLoad(EventArgs e) +67
       System.Web.UI.Control.LoadRecursive() +35
       System.Web.UI.Page.ProcessRequestMain() +750
    
    Here is the syntax I'm using (asp.net 1.1, vb) in my page load, if not posting back:



    txtUN.Text = Request.Cookies("UsernameCookie").Value


    I also use forms authentication. if the logon is good (checked against SQL server table), I do this:


    Dim ticket As FormsAuthenticationTicket = New FormsAuthenticationTicket(UName, False, 5000)


    '*** add cookie for user name
    Response.Cookies("UserNameCookie").Value = UName
    FormsAuthentication.RedirectFromLoginPage(UName, chkPersistCookie.Checked)







    Ali Ibarguen
    BluePrint PM, LLC
    blueprintpm.com
     
  2. The error is due to the fact that 'UsernameCookie' does not exist.Create cookies as follows:

    HttpCookie cookie;
    cookie = new HttpCookie('UsernameCookie', 'daspstaff');
    cookie.Expires = DateTime.Now.AddDays(30);
    Response.Cookies.Add(cookie);

    Vikram

    DiscountASP.NET
    www.DiscountASP.NET
     
  3. Vikram, I think I love you. That was it- just in time for my clientdemo to sell my product tomorrow morning!

    Thanks so much - Alison
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page