i have a class in app_code folder which implements the Ihttphandler when the execution enter in the Processrequest method i have to use the session variables created on login page.but there is no such Session Object present there.

on login page i hav created session like this

session("userid")=dr("user")

i hav context which is object of Httpcontext class which is a input parameter in the Processrequest method.

with this context object i have wrote

dim user as string =context.Session.Item("userid")

but this will genearte an error

An exception of type 'System.NullReferenceException' occurred in App_Code.7qviugdw.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.


how can access session inside this class?

Dani AI

Generated

got a NullReferenceException because the handler ran without session state being available. 's tip about using the global HttpContext is fine, but it doesn't by itself enable session for an IHttpHandler. By default an IHttpHandler does not participate in ASP.NET session state unless it opts in.

You need to make the handler declare the session-state marker interface so ASP.NET wires session state before ProcessRequest runs. If the handler only reads session (no writes) use the read-only marker to avoid locking. Also always null-check context.Session at runtime — session can still be missing if sessionState is turned off or the request doesn't carry the session cookie.

Example (C#) — the important bit is the marker interface and a runtime check:

using System.Web;
using System.Web.SessionState;

public class MyHandler : IHttpHandler, IRequiresSessionState
{
    public bool IsReusable => false;

    public void ProcessRequest(HttpContext context)
    {
        if (context == null || context.Session == null)
        {
            // session not available — handle gracefully
            return;
        }

        var uidObj = context.Session["userid"];
        var userId = uidObj != null ? uidObj.ToString() : null;
        // use userId...
    }
}

Quick troubleshooting checklist:

  • Implement IRequiresSessionState (or IReadOnlySessionState if only reading).
  • Confirm <sessionState> in web.config is not Off and the ASP.NET_SessionId cookie is sent.
  • Ensure the login code actually ran before this handler is invoked (same session ID).
  • If still null, verify the handler is registered for the correct pipeline (system.web vs system.webServer) and not being invoked for static requests.
  • Be aware of session locking: prefer read-only marker or avoid heavy session use in frequently-called handlers.

After adding the marker interface and doing the checks above, the NullReferenceException seen by should be resolved; ’s suggestion to use the global context becomes usable only once session state is enabled.

i have a class in app_code folder which implements the Ihttphandler when the execution enter in the Processrequest method i have to use the session variables created on login page.but there is no such Session Object present there.

on login page i hav created session like this

session("userid")=dr("user")

i hav context which is object of Httpcontext class which is a input parameter in the Processrequest method.

with this context object i have wrote

dim user as string =context.Session.Item("userid")

but this will genearte an error

An exception of type 'System.NullReferenceException' occurred in App_Code.7qviugdw.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.


how can access session inside this class?

You can use HttpContext.Current.Session() Instead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.