8

If I store a value in a session variable

    Session["Int"] = 100;

What it will be in the Session_End event? Will it be null or 100?

void Session_End(object sender, EventArgs e)
{
      object objInt = Session["Int"];          // Null or 100 ?
}

Meaning, will Session_End fire after disposing everything in the session or just before?

Chad
  • 1,531
  • 3
  • 20
  • 46
v s
  • 549
  • 2
  • 7
  • 17
  • 3
    Why don't you put a breakpoint on that method and see what is the value when the breakpoint is hit? – RePierre Sep 06 '12 at 07:03
  • also check this for HttpContext.Current http://stackoverflow.com/questions/2030113/asp-net-session-variables-on-session-end – MSTdev May 03 '17 at 10:58

2 Answers2

13

It is 100.

To test it yourself simply add the ASP.NET application file global.asax to your project and handle the Session_Start end Session-End events:

void Session_Start(object sender, EventArgs e)
{
   Session["Int"] = 100;          // 100
}

void Session_End(object sender, EventArgs e)
{
    object objInt = Session["Int"];  // it is still 100 here
}

You can end a Session by Session.Abandon() (or when it expires).

protected void Page_Load(object sender, EventArgs e)
{
    Session.Abandon();  // after this Session.End is called
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • also check this for HttpContext.Current http://stackoverflow.com/questions/2030113/asp-net-session-variables-on-session-end – MSTdev May 03 '17 at 10:58
0

I found that Session["Int"] will be 100. I set the session timeout to just 1 minute and put a break point in that event.

Federico Sierra
  • 5,118
  • 2
  • 23
  • 36
v s
  • 549
  • 2
  • 7
  • 17
  • well when i posted my answer somebody has already answered it so it was a coincident – v s Sep 06 '12 at 07:17