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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:04:09+00:00 2026-06-09T20:04:09+00:00

Here Scenario is HttpContext.Current.Session[value].ToString() gives null value, even have already set session value when

  • 0

Here Scenario is HttpContext.Current.Session[“value”].ToString() gives null value, even have already set session value when user login.

My webconfig

<sessionState timeout="40" mode="InProc"/>

My global.asax

   void Session_Start(object sender, EventArgs e) 
    {
       // Code that runs when a new session is started
       Session["EmployeeId"] = "";
       this.Session["DomainName"] = "";
    }

In my Defaultpage.asppx.cs

Emp_grade empgrd = new Emp_grade(); gives Object reference not set to an instance of an object

using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Configuration;

public partial class EmpGrade : System.Web.UI.Page
{

   //here am getting error(The type initializer for 'Emp_grade' threw an exception)
   // stack error message  Object reference not set to an instance of an object.
   Emp_grade empgrd = new Emp_grade(); 

   protected void Page_Load(object sender, EventArgs e)
    {
      logic code...
      Datatable dt= empgrd.EmpRecord();
    }
}

In my App_Code folder i have class file Emp_grade.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Web.SessionState;

public class Emp_grade  
{       
    public Emp_grade()
    {
        //TODO: Add constructor logic here
    }

    static string getConnection = HttpContext.Current.Session["DomainName"].ToString();
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[getConnection].ConnectionString);

     public DataTable EmpRecord()
    {
       logic code
    }
 }

My login page:

 protected void Page_Load(object sender, EventArgs e)
    { 
    //set some value
     Session["DomainName"] = domainname;
    }

ScreenShot(Debugging)
enter image description here

  • 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-06-09T20:04:10+00:00Added an answer on June 9, 2026 at 8:04 pm

    You should probably refactor a bit. This:

    static string getConnection = HttpContext.Current.Session["DomainName"].ToString(); 
    

    is a static field with an initializer. It will run once, and keep whatever value it got until the application is restarted. And it will run at a time that is not very well defined, it is only guranteed that it will run sometime before it is first accessed.

    This means that it may well run at a time when no HttpContext is present (like Dan Puzey wrote) which will cause an exception to be thrown. Any subsequent attempt to access it will (to the best of my knowledge) result in the same Exception being thrown.

    But keep in mind that it is only fetched once, so even if the initializer were to succeed and find a value, that value will be used for all subsequent uses of getConnection. It will not be fetched anew for each different user.

    To fix that, you could wrap it in an instance method:

    private string GetDomainName()
    {
        return HttpContext.Current.Session["DomainName"].ToString();
    }
    SqlConnection conn = null;
    

    and then initialize conn in the constructor:

    public Emp_grade()         
    {         
        conn = new SqlConnection(ConfigurationManager.ConnectionStrings[GetDomainName()].ConnectionString);
    }
    

    Please also note the since this class is initializing and storing a SqlConnection (which is IDisposable), your class should also implement the IDisposable interface. For background information about implementing IDisposable, see for instance this answer.

    EDIT: The screenshot shows that the HttpContext.Current.Session is returning null.

    The reason is this line:

    Emp_grade empgrd = new Emp_grade();
    

    This is declaring an instance member in the Page class, with an initializer. The initializer is run very early in the request pipeline, so at the point when it runs the Session is not yet present in the context.

    Change to:

    Emp_grade empgrd = null;
    
    protected void Page_Load(object sender, EventArgs e) 
    { 
      empgrd = new Emp_grade();
      Datatable dt = empgrd.EmpRecord(); 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the scenario. When a user's login info is submitted, activityIndicator view is shown.
Here's the scenario: The user has set that is attending an event, I want
I am new to Win Forms, I have a scenario here.. When User enter
Here is my scenario: I have two MySQL tables: Categories (columns: id, category) Items
Here's the scenario: I have a public repo A . Bob forks A ,
Here's the scenario I anticipate: I have an app written in PHP which has
I have a rather odd scenario here, hoping someone might have some insight. I
I am new to design patterns and I have a scenario here. I am
Here is the scenario: I have 1 master page and web-forms in that master
Here's the scenario. We have a router that port forwards requests for our different

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.