Getting error while setting session value

Discussion in 'ASP .Net' started by sony.m.2007, Jun 12, 2008.

  1. sony.m.2007

    sony.m.2007 Guest

    Hi,
    When i try to set a value for a session variable I'm getting a object
    refence not set error

    I tried two methods as below
    HttpContext.Current.Session.Add("AppStartTime", DateTime.Now);
    HttpContext.Current.Session["AppStartTime"] = DateTime.Now;

    For the above two methods I'm getting error as
    "Object reference not set to an instance of an object."
    Kindly help me out.

    Thanks,
    Sony
     
    sony.m.2007, Jun 12, 2008
    #1
    1. Advertisements

  2. sony.m.2007

    Manish Guest

    Hi Sony,

    I tried the code and it works fine.

    protected void Page_Load(object sender, EventArgs e)
    {
    HttpContext.Current.Session["AppStartTime"] = DateTime.Now;
    Response.Write(Session["AppStartTime"].ToString());
    }

    Regards,
    Manish
    www.componentone.com
     
    Manish, Jun 12, 2008
    #2
    1. Advertisements

  3. sony.m.2007

    sony.m.2007 Guest

    Hi,
    My class implements IHttpModule
    and I'm assigning the session value at onbeginrequest method

    public class TestHTTPModule : IHttpModule
    {
    public void Init(HttpApplication application)
    {
    application.BeginRequest +=
    (new EventHandler(this.Application_BeginRequest));
    }
    private void Application_BeginRequest(Object source,
    EventArgs e)
    {
    HttpContext.Current.Session.Add("AppStartTime",
    DateTime.Now);
    HttpContext.Current.Session["AppStartTime"] = DateTime.Now;
    }
    }

    If u try like above you will get the error message.
    How to solve this?

    Thanks,
    Sony
     
    sony.m.2007, Jun 12, 2008
    #3
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.