Reputation: 848
When a user logs into the website, I am creating 5 session variables with some user related values (like userid
,roleid
etc..). Based on one of the values stored in a session variable, I have to update a record in the database. To do this I am writing the following code in the Session_End()
event of Global.asax
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 fails and the record in the database is not updated. When this is tested in my local system it works fine but when I test this in production server some of the records are not getting updated
Why is this happening?
Some one please explain how the Session_End
works exactly.
I have searched everywhere but can't find a solution to this.
Upvotes: 1
Views: 6091
Reputation: 848
If I declare a variable for each session value I want to access in the Session_End event and store the value of the session in this variable and use the declared variable in the my functionality it is working fine
void Session_End(object sender, EventArgs e)
{
int var=0;
if (Session["value"] != null)
{
var = Convert.ToInt32(Session["value"]);
UpdateToDatabase(var);
}
}
Upvotes: 0
Reputation: 1841
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 !!
Upvotes: 1