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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:34:12+00:00 2026-05-20T16:34:12+00:00

I have a master page that set ups some variables that I want to

  • 0

I have a master page that set ups some variables that I want to use across the site..

protected void Page_Load(object sender, EventArgs e)
{
    //Get users name from AD
    str_DomainName = HttpContext.Current.User.Identity.Name;
    str_CurrentLogin = str_DomainName.Substring(5);

    //Display current user information
    DirectorySearcher search = new DirectorySearcher("LDAP://DCHS");
    search.Filter = String.Format("(SAMAccountName={0})", str_CurrentLogin);
    SearchResult result = search.FindOne();
    DirectoryEntry entry = result.GetDirectoryEntry();
    lbl_CurrentUser.Text = result.Properties["givenName"][0].ToString() + ' ' + result.Properties["sn"][0].ToString();

    // Get SID
    IntPtr logonToken = WindowsIdentity.GetCurrent().Token;
    WindowsIdentity windowsId = new WindowsIdentity(logonToken);

    //Set session variabls
    this.CurrentFirstName = result.Properties["givenName"][0].ToString();
    //this.CurrentEmail = result.Properties["mail"][0].ToString();
    //this.CurrentSID = windowsId.User.ToString();
    //this.CurrentUserName = str_CurrentLogin;
    //this.CurrentFullName = lbl_CurrentUser.Text;
    //this.CurrentDomain = str_DomainName;
    this.Session.Add("currentEmail", result.Properties["mail"][0].ToString());
}

public String CurrentFirstName
        {
            get { return (String)ViewState["currentFirstName"]; }
            set { ViewState["currentFirstName"] = value; }
        }

I then call them in my defalut.aspx page as follows:

protected void Page_PreRender(object sender, EventArgs e)
        {
            //try
            //{
                lbl_FullName.Text = Master.CurrentFullName;
                lbl_SID.Text = Master.CurrentSID;
                testLabel.Text = Master.CurrentEmail;
            //}
            //catch (Exception ex)
            //{ }
        }

This works fine.. If I however navigate away from the default page, then I get the following error..

Object reference not set to an instance of an object.

One the lbl_FullName.Text = Master.CurrentFullName; line

If I uncomment the try catch then it works fine, but I don’t believe that this is the correct method of avoiding the fault..

I’m only new to ASP, so be nice..

EDIT:

The variabes are being set in Master.cs as follows.

public String CurrentUserName
        {
            get { return (String)ViewState["currentUserName"]; }
            set { ViewState["currentUserName"] = value; }
        }
  • 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-20T16:34:12+00:00Added an answer on May 20, 2026 at 4:34 pm

    ViewState is per-page. When you navigate to the new page, it isn’t set.

    Your master page should put these into Session instead.

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

Sidebar

Related Questions

I have a view using a master page that contains some javascript that needs
I have an ASP.NET Site that has a single Master Page. On one of
I have a master page that has two content sections like this (left some
Currently I have a site that is set up using a masterpage and a
I have a master page that contains an ASP.NET server side Menu control (System.Web.UI.WebControls.Menu)
I'd like to have a page that uses a child master page, fill in
I have a dropdown on my master page that lets the user switch language
I have a controller's action and view page that uses a master page. The
I have 2 master pages that are nested.this is main master page code for
I have two pages that inherit from one master page, First.aspx or second.aspx. Navigation

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.