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)

You should probably refactor a bit. This:
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
HttpContextis 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:
and then initialize
connin the constructor:Please also note the since this class is initializing and storing a
SqlConnection(which isIDisposable), your class should also implement theIDisposableinterface. For background information about implementingIDisposable, see for instance this answer.EDIT: The screenshot shows that the
HttpContext.Current.Sessionis returning null.The reason is this line:
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: