Session End
-
I've got a problem: When user enter a webpage or login that means a Session for that user created and when user close IE browser or log out of web page or something like that that means Session Ended In file Global.asax.cs and in Session_End event i wrote a segment of code to log out users automatically when users turn off the IE browser but it haven't worked. I want to ask: Session_End event raises before of after the real session ends ? I think it raise before the real session ends but if it happens so my segment of code must have worked protected void Session_End(Object sender, EventArgs e) { SqlConnection con = new SqlConnection(ThongTin.CONNECTIONSTRING); SqlCommand command = new SqlCommand(); command.Connection = con; command.CommandText = "UPDATE HOCVIEN SET Online = 0 WHERE MaHocVien = @MaHocVien"; command.Parameters.Add("@MaHocVien",SqlDbType.Char,10); command.Parameters["@MaHocVien"].Value = Session["MaHocVien"].ToString(); try { con.Open(); command.ExecuteNonQuery(); } catch(Exception ex) { Response.Write(ex.Message); Response.Write(ex.StackTrace); } finally { con.Close(); command.Dispose(); } } Thanks in advance
-
I've got a problem: When user enter a webpage or login that means a Session for that user created and when user close IE browser or log out of web page or something like that that means Session Ended In file Global.asax.cs and in Session_End event i wrote a segment of code to log out users automatically when users turn off the IE browser but it haven't worked. I want to ask: Session_End event raises before of after the real session ends ? I think it raise before the real session ends but if it happens so my segment of code must have worked protected void Session_End(Object sender, EventArgs e) { SqlConnection con = new SqlConnection(ThongTin.CONNECTIONSTRING); SqlCommand command = new SqlCommand(); command.Connection = con; command.CommandText = "UPDATE HOCVIEN SET Online = 0 WHERE MaHocVien = @MaHocVien"; command.Parameters.Add("@MaHocVien",SqlDbType.Char,10); command.Parameters["@MaHocVien"].Value = Session["MaHocVien"].ToString(); try { con.Open(); command.ExecuteNonQuery(); } catch(Exception ex) { Response.Write(ex.Message); Response.Write(ex.StackTrace); } finally { con.Close(); command.Dispose(); } } Thanks in advance
The problem with SessionEnd is that it doesn't occur when you close the browser, but the session ends which can be 20 minutes after you close the browser or more (depends on your web.config). Anyway, it will happen it just takes time.