Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should probably refactor a bit. This:</p> <pre><code>static string getConnection = HttpContext.Current.Session["DomainName"].ToString(); </code></pre> <p>is a static field with an initializer. It will run <strong>once</strong>, 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.</p> <p>This means that it may well run at a time when no <code>HttpContext</code> is present (like <strong>Dan Puzey</strong> 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.</p> <p>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 <code>getConnection</code>. It will not be fetched anew for each different user.</p> <p>To fix that, you could wrap it in an instance method:</p> <pre><code>private string GetDomainName() { return HttpContext.Current.Session["DomainName"].ToString(); } SqlConnection conn = null; </code></pre> <p>and then initialize <code>conn</code> in the constructor:</p> <pre><code>public Emp_grade() { conn = new SqlConnection(ConfigurationManager.ConnectionStrings[GetDomainName()].ConnectionString); } </code></pre> <p>Please also note the since this class is initializing and storing a <code>SqlConnection</code> (which is <code>IDisposable</code>), your class should also implement the <code>IDisposable</code> interface. For background information about implementing <code>IDisposable</code>, see for instance <a href="https://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface/538238#538238">this answer</a>.</p> <p><strong>EDIT</strong>: The screenshot shows that the <code>HttpContext.Current.Session</code> is returning null. </p> <p>The reason is this line:</p> <pre><code>Emp_grade empgrd = new Emp_grade(); </code></pre> <p>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.</p> <p>Change to:</p> <pre><code>Emp_grade empgrd = null; protected void Page_Load(object sender, EventArgs e) { empgrd = new Emp_grade(); Datatable dt = empgrd.EmpRecord(); } </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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