Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy HttpContext.Current.Session["value"].ToString() gives null value inside App_Code class file, Object reference not set to an instance of an object
    primarykey
    data
    text
    <p>Here Scenario is HttpContext.Current.Session["value"].ToString() gives null value, even have already set session value when user login.</p> <p><strong>My webconfig</strong></p> <pre><code>&lt;sessionState timeout="40" mode="InProc"/&gt; </code></pre> <p><strong>My global.asax</strong></p> <pre><code> void Session_Start(object sender, EventArgs e) { // Code that runs when a new session is started Session["EmployeeId"] = ""; this.Session["DomainName"] = ""; } </code></pre> <p><strong>In my Defaultpage.asppx.cs</strong></p> <p><code>Emp_grade empgrd = new Emp_grade();</code> gives Object reference not set to an instance of an object</p> <pre><code>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(); } } </code></pre> <p>In my <code>App_Code folder</code> i have class file <code>Emp_grade.cs</code></p> <pre><code>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 } } </code></pre> <p><strong>My login page:</strong></p> <pre><code> protected void Page_Load(object sender, EventArgs e) { //set some value Session["DomainName"] = domainname; } </code></pre> <p><strong>ScreenShot(Debugging)</strong> <img src="https://i.stack.imgur.com/ZXPO3.jpg" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload