Search code examples
c#sessionsession-variablessession-state

How to get a value of a session key value pair in Session_OnEnd() method


I have added a value of an Order using Session.Add("order",1) in Session so when user logs out I can unlock the order which I locked when user accessing it. Is there any other way to fire a database query when user logs out ? Please help.


Solution

  • You could have your code in the Session_End method, but only under the following condition:

    The Session_OnEnd event is only supported when the session-state HttpSessionState.Mode property value is InProc, which is the default. If the session-state Mode is set to StateServer or SQLServer, then the Session_OnEnd event in the Global.asax file is ignored. If the session state Mode property value is Custom, then support for the Session_OnEnd event is determined by the custom session-state store provider.

    If you are using SQLServer or StateServer for session management - you can implement an HttpModule.

    See the following for reference:

    http://www.codeproject.com/Articles/21156/ASP-NET-HttpModule-for-handling-session-end-with-S

    http://www.ivan-nikolov.com/en/article/5/asp-net-session-state-mode-and-session-end-callback