---
title: "Session variable value not always available in Global.asax Session_end"  
description: "Session variable value not always available in Global.asax Session_end"  
author: "Anonymous User"  
published: 2014-08-31  
updated: 2014-09-01  
canonical: https://www.mindstick.com/forum/2211/session-variable-value-not-always-available-in-global-asax-session_end  
category: "asp.net"  
tags: ["asp.net", "c#", ".net", "mvc4", "asp.net mvc", "session", "session variable", "Session_end", "get session variable value"]  
reading_time: 1 minute  

---

# Session variable value not always available in Global.asax Session_end

When a user logs into the website, I am creating 5 session [variables](https://www.mindstick.com/articles/715/php-variables) with some user related values (like userid,roleid etc..). Based on one of the values stored in a [session variable](https://www.mindstick.com/forum/12804/how-to-set-a-asp-dropdownlist-selectedvalue-to-a-session-variable), I have to update a record in the [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax). To do this I am writing the following code in the Session_End() event of [Global.asax](https://www.mindstick.com/interview/1833/what-are-the-event-handlers-that-can-be-included-in-the-global-asax-file)

if (Session["UserHistoryID"] != null)

SessionUtility.UpdateDatabase(Convert.ToInt32(Session["UserHistoryID"]));

The problem here with this is when the Session times out and Session_End fires the Session variable Session["UserHistoryID"] is NULL some-time and the above [condition](https://www.mindstick.com/forum/12711/select-query-with-and-condition) fails and the record in the database is not updated. When this is tested in my local system it [works fine](https://answers.mindstick.com/qa/115732/my-software-works-fine-on-windows-10-but-crashes-on-windows-11-why) but when I test this in [production server](https://www.mindstick.com/forum/159285/describe-how-you-would-deploy-a-mern-stack-application-to-a-production-server) some of the records are not getting updated

Some one [please explain](https://www.mindstick.com/forum/344/hi-rohith-i-am-new-for-mvc-please-explain-how-to-set-implicitly-isauthenticated-true) how the Session_End works exactly.

\

## Replies

### Reply by Sumit Kesarwani

I think that the earliest possible time you could obtain the Session value is under Application_AcquireRequestState as mentioned hereSessions and Global.asax so you could write something like this in your Global.asax

void Application_AcquireRequestState(object sender, EventArgs e)

{

if (System.Web.HttpContext.Current.Session != null)

{

if (Session["language"] != null)

{

Response.Write(Session["language"].ToString());

}

}

}

hopefully that would help !!


---

Original Source: https://www.mindstick.com/forum/2211/session-variable-value-not-always-available-in-global-asax-session_end

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
