Hi all

I have declared a Session variable in web handler i-e .ashx file like below

int SaleID;
  HttpContext.Current.Session["tempSaleID"] = SaleID;

Now i want to remove this Session variable in class file i-e .cs

In normal cases Session variable are removed using

Session.Remove("SaleID");

but in this case its not working because its declared in web handler.

Kindly tell me how can i remove this?

Dani AI

Generated

Brief answer tied to the thread: was right — you must remove the same key you set (the handler used tempSaleID), and the code that removes it must run where an ASP.NET session object is available. Two common failure modes are (a) trying to remove the wrong key and (b) running the removal code where HttpContext.Current (and therefore Session) is null or not enabled for the handler.

Safest removal from within request code:

var ctx = System.Web.HttpContext.Current;
if (ctx != null && ctx.Session != null)
{
    ctx.Session.Remove("tempSaleID");
}

If the removal is happening inside a helper class or a background thread, avoid relying on HttpContext.Current. Instead accept the session object from the caller and remove the key there:

public static void ClearTempSaleId(System.Web.SessionState.HttpSessionState session)
{
    if (session == null) return;
    session.Remove("tempSaleID");
}

If the code that sets/reads the session runs in an .ashx handler, make sure the handler requests session access; otherwise Session will not be available. Implement IRequiresSessionState on the handler class so session state is provided during requests:

public class MyHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        // context.Session is available here
    }
    public bool IsReusable => false;
}

To read the session identifier use the SessionID property of the session object. For reference see the Microsoft docs for HttpSessionState.Remove, IRequiresSessionState and HttpSessionState.SessionID.

if u wanted to destroy the session then u can write the code like.
session["sessioid"]="";
your session will automatically destroied.

How can i get Sessionid

SessionID is your session name

use session.abandon() method will destroy your session

seems like you are removing the wrong session name. your session name in the web handler is tempSaleID and what you are trying to remove is SaleID

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.