Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 618277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:31:38+00:00 2026-05-13T18:31:38+00:00

OK so to set and read variables from the current session String Myvar =(string)

  • 0

OK so to set and read variables from the current session

String Myvar =(string) System.Web.HttpContext.Current.Session[“MyVariable”]

To set

System.Web.HttpContext.Current.Session[“MyVariable”] = “NewValue”

I can do neither, I get a System.NullReferenceException: Object reference not set to an instance of an object. from System.Web.HttpContext.Current.Session.

In my web.config I have

<sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" cookieless="false" timeout="20">
</sessionState>  

I have read a dozen articles on the the necessity of IHttpHandler and an IRequiresSessionState interface. I think the issue may be caused because I am requesting this information in Page_PreInit. I found a solution in a stack overflow article but I don’t seem be using it properly to actually make this go.

I am not sure what I am missing. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:31:39+00:00Added an answer on May 13, 2026 at 6:31 pm

    To access the session state pre-init you can do something like this. I use it so that I can have a different admin master than the regular user one. Each page has a method at the top.

    PageTools tools = new PageTools();
    protected void Page_PreInit(object sender, EventArgs e)
    {
        tools.setMasterPage(Page, Context);
    }
    

    PageTools is my class that holds the method that chooses the appropriate mater page and has the http handler.

    public void setMasterPage(Page page, HttpContext context)
        /***********************************************************************
         * Author   Daniel Tweddell
         * Date     9/18/09
         * 
         * Several of the pages are for non-admin use, however these pages will
         * also be used by the admin users and will need to have the admin menu
         * and such.  So based on the login, we either show the page with the
         * standard master or if the user is admin, use the admin master.
         ***********************************************************************/
        {
            if (context.Handler is IReadOnlySessionState || context.Handler is IRequiresSessionState)
            {
                context.Handler = Handler();
            }
            String sMasterPage="~/content/page.master";
            if (userinfo.IsUserAdmin) sMasterPage="~/content/administrator/admin.master";//make sure the user is admin
            page.MasterPageFile = sMasterPage; 
        }
    

    Here is a step by step to setting up the httphandler. (which is the other thing you’ll need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 423k
  • Answers 423k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to grab a reference to the instance's singleton… May 15, 2026 at 11:29 am
  • Editorial Team
    Editorial Team added an answer Supporting all HTTP methods: sed 's#.*\(\[[^]]*\]\).*"[A-Z]* \(.*\) HTTP/[0-9.]*" \(.*\)#\1 \2… May 15, 2026 at 11:29 am
  • Editorial Team
    Editorial Team added an answer pre-commit hook is the only way that I find to… May 15, 2026 at 11:29 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.