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 7015423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:39:00+00:00 2026-05-27T22:39:00+00:00

I’m somewhat new to ASP / C# and I’m having an issue (probably simple)

  • 0

I’m somewhat new to ASP / C# and I’m having an issue (probably simple) with sessions variables. My project has a Site.Master in which session variables are set under the Page_Load method like so:

    protected void Page_Load(object sender, EventArgs e)
    {

        if ((Session)["UserID"]==null || (Session)["UserID"].ToString() == "")
        {
            (Session)["UserID"] = HttpContext.Current.User.Identity.Name.ToString();
            SqlDataReader dr = Sprocs.GetPermissionGroups();

            string groupList = "";

            while (dr.Read())
            {
            if (groupList != "")
                    {
                        groupList = groupList + "|" + dr["WG_Group"].ToString();
                    }
                    else
                    {
                        groupList = dr["WG_Group"].ToString();
                    }
            }
            dr.Close();

            if (groupList != "")
            {
                (Session)["UserGroups"] = groupList;
            }
        }

This does work. If I dump out the session variable ‘UserGroups’ to a label or something within this method, it does display the variable contents correctly.

So, my problem lies within another page (say default.aspx) when I try to access that same session variable. In the Page_Load method of the other page I attempt to do this:

    protected void Page_Load(object sender, EventArgs e)
    {
            string GroupList = HttpContext.Current.Session["UserGroups"].ToString();
            //some code with the variables here
    }

This always fails with an “Object reference not set to an instance of an object.” error. Am I trying to get the Session variable wrong? I’ve tried

string GroupList = Session["UserGroups"].ToString(); 

this also errors with the same error.

string GroupList = (string)(Session["UserGroups"]); 

This always returns an empty string.

What am I doing wrong?

Thanks!

  • 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-27T22:39:01+00:00Added an answer on May 27, 2026 at 10:39 pm

    The syntax you are using in your Page_Load method I wouldn’t have even expected to compile. Regardless, the issue is that you have not set the Session with that key, so it will return null. When you call ToString() on that null, you get the exception. In your second example:

    string groupList = (string)(Session["UserGroups"])
    

    That is doing a conversion of null to string, which results in a null string (thus not causing the exception).

    You should be able to rewrite your Page_Load implementation like so:

    string sessionUserId = Session["UserId"] as string;
    
    if(string.IsNullOrEmpty(sessionUserId))
    {
        Session["UserId"] = HttpContext.Current.User.Identity.Name.ToString();
    
        SqlDataReader dr = Sprocs.GetPermissionGroups();
    
        string groupList = "";
    
        while (dr.Read())
        {
            if (groupList != "")
            {
                groupList = groupList + "|" + dr["WG_Group"].ToString();
            }
            else
            {
                groupList = dr["WG_Group"].ToString();
            }
        }
        dr.Close();
    
        Session["UserGroups"] = groupList;
    }
    

    Then, when accessing the session variable later, do it like so:

     string userGroup = Session["UserGroups"] as string;
    

    This is a safe way to attempt the conversion of whatever bucket that is in session into a string. If the key doesnt exist, or the value is not a string, you will get null. Otherwise, you will get the string from that hash.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.