0

Hey all I am trying to set up a Session that wont turn to NULL whenever I just from page to page.

I found THIS page here on Stackoverflow and it's the one I am currently trying to get working. The post by @M4N is the one I am talking about.

My code is this:

HELPer.cs:

public class SessionWrapper
{
    private SessionWrapper()
    {
    }

    public static SessionWrapper Instance
    {
        get
        {
            SessionWrapper wrapper = (SessionWrapper)HttpContext.Current.Session["SESSION_MANAGER"];

            if (wrapper == null)
            {
                wrapper = new SessionWrapper();
                HttpContext.Current.Session["SESSION_MANAGER"] = wrapper;
            }

            return wrapper;
        }
    }

    public string _lastFirstMiddle { get; set; }
    public string _company { get; set; }
    public string _userPrincipalName { get; set; }
    public string _sAMAccountName { get; set; }
    public string _displayName { get; set; }
    public string _department { get; set; }
    public string _departmentNumber { get; set; }
    public string _physicalDeliveryOfficeName { get; set; }
    public string _division { get; set; }
    public string _employeeID { get; set; }
    public string _givenName { get; set; }
    public string _mail { get; set; }
    public string _name { get; set; }
    public string _sAMAccountType { get; set; }
    public string _sn { get; set; }
    public string _title { get; set; }
    public int _userID { get; set; }
    public string _userAMT { get; set; }
    public List<string> _perms { get; set; }

    public static string lastFirstMiddle { get { return Instance._lastFirstMiddle; }set { Instance._lastFirstMiddle = value; }}
    public static string company { get { return Instance._company; } set { Instance._company = value; } }
    public static string userPrincipalName { get { return Instance._userPrincipalName; } set { Instance._userPrincipalName = value; } }
    public static string sAMAccountName { get { return Instance._sAMAccountName; } set { Instance._sAMAccountName = value; } }
    public static string displayName { get { return Instance._displayName; } set { Instance._displayName = value; } }
    public static string department { get { return Instance._department; } set { Instance._department = value; } }
    public static string departmentNumber { get { return Instance._departmentNumber; } set { Instance._departmentNumber = value; } }
    public static string physicalDeliveryOfficeName { get { return Instance._physicalDeliveryOfficeName; } set { Instance._physicalDeliveryOfficeName = value; } }
    public static string division { get { return Instance._division; } set { Instance._division = value; } }
    public static string employeeID { get { return Instance._employeeID; } set { Instance._employeeID = value; } }
    public static string givenName { get { return Instance._givenName; } set { Instance._givenName = value; } }
    public static string mail { get { return Instance._mail; } set { Instance._mail = value; } }
    public static string name { get { return Instance._name; } set { Instance._name = value; } }
    public static string sAMAccountType { get { return Instance._sAMAccountType; } set { Instance._sAMAccountType = value; } }
    public static string sn { get { return Instance._sn; } set { Instance._sn = value; } }
    public static string title { get { return Instance._title; } set { Instance._title = value; } }
    public static int userID { get { return Instance._userID; } set { Instance._userID = value; } }
    public static string userAMT { get { return Instance._userAMT; } set { Instance._userAMT = value; } }
    public static List<string> perms { get { return Instance._perms; } set { Instance._perms = value; } }
}

And this is how I initiate it:

SessionWrapper.Instance._lastFirstMiddle = de.Properties["cn"].Value.ToString();
SessionWrapper.Instance._company = de.Properties["company"].Value.ToString();
SessionWrapper.Instance._userPrincipalName = de.Properties["userPrincipalName"].Value.ToString();
SessionWrapper.Instance._sAMAccountName = de.Properties["sAMAccountName"].Value.ToString();
SessionWrapper.Instance._displayName = de.Properties["displayName"].Value.ToString();
SessionWrapper.Instance._department = de.Properties["department"].Value.ToString();
SessionWrapper.Instance._departmentNumber = de.Properties["departmentNumber"].Value.ToString();
SessionWrapper.Instance._division = de.Properties["division"].Value.ToString();
SessionWrapper.Instance._employeeID = de.Properties["employeeID"].Value.ToString();
SessionWrapper.Instance._title = de.Properties["title"].Value.ToString();
SessionWrapper.Instance._givenName = de.Properties["givenName"].Value.ToString();
SessionWrapper.Instance._name = de.Properties["name"].Value.ToString();
SessionWrapper.Instance._sAMAccountType = de.Properties["sAMAccountType"].Value.ToString();
SessionWrapper.Instance._sn = de.Properties["sn"].Value.ToString();
SessionWrapper.Instance._physicalDeliveryOfficeName = de.Properties["physicalDeliveryOfficeName"].Value.ToString();
SessionWrapper.Instance._perms = allData.Tables[0].Rows[0]["permissions"].ToString().Split('|').ToList<string>();
SessionWrapper.Instance._userID = (int)allData.Tables[0].Rows[0]["id"];

However, I am receiving this error of:

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

on this line:

SessionWrapper wrapper = (SessionWrapper)HttpContext.Current.Session["SESSION_MANAGER"];

So I am not really sure what all I need to do in order to correct this error so any help would be great!

[Update 1]

enter image description here

DavidG
  • 113,891
  • 12
  • 217
  • 223
StealthRT
  • 10,108
  • 40
  • 183
  • 342
  • @mjwills the **HttpContext** I can not seem to get a value for. The **HttpContext.Current** value seems to be **empty** and **HttpContext.Current.Session** is **null** – StealthRT Aug 14 '17 at 13:40
  • Yes, it is a dupe. The other question is "What is a NullReferenceException, and how do I fix it?" and that's exactly what you are asking here. – DavidG Aug 14 '17 at 13:43
  • @mjwills Well yes and no. The dupe target is still correct, but that link does add a little context so I've added it to the dupe target list – DavidG Aug 14 '17 at 14:06

0 Answers0