这里的场景是 HttpContext.Current.Session["value"].ToString() 给出空值,甚至在用户登录时已经设置了会话值。
我的网络配置
<sessionState timeout="40" mode="InProc"/>
我的全球.asax
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Session["EmployeeId"] = "";
this.Session["DomainName"] = "";
}
在我的 Defaultpage.asppx.cs
Emp_grade empgrd = new Emp_grade();
给出未设置为对象实例的对象引用
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();
}
}
在我的App_Code folder
我有类文件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
}
}
我的登录页面:
protected void Page_Load(object sender, EventArgs e)
{
//set some value
Session["DomainName"] = domainname;
}
截图(调试)