Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote below code in global.asax(application_end) whenever i closed the browser and reopens the session values are not clearing and status is showing null in database.

C#
string strlogout = "logout";
          //updatelogouttime();
    //     public void updatelogouttime()
    //{
        Empbusinessaccesslayer obj = new Empbusinessaccesslayer();
        obj.Empname = Session["Username"].ToString();
        obj.Ipaddress = Session["ipaddress"].ToString();
        DateTime dt = (DateTime)Session["logintime"];
        obj.Createddate = Convert.ToDateTime(Session["logindate"].ToString());
        obj.Jobposteddate = dt;
        obj.Comptypeid = int.Parse(Session["comptypeid"].ToString());
        obj.Curremployer = strlogout;
        obj.updateuserlogout(Session["Username"].ToString(), Session["ipaddress"].ToString(), Convert.ToDateTime(Session["logindate"].ToString()), dt, int.Parse(Session["comptypeid"].ToString()), strlogout);

        Session["Username"] = null;
        Session["empname"] = null;
        Session["comptypeid"] = null;
        Session.Contents.Clear();
        Session.Abandon();
Posted
Updated 2-Jul-12 21:17pm
v2

Alternatively you can have a look at following link from CP:
How to end user session when browser closed[^]
 
Share this answer
 
Comments
Prasad_Kulkarni 3-Jul-12 3:37am    
5'ed
clear session values when the browser is closed
Closing the browser should actually clear the session variables[^]

You can add update query on window.onbeforeunload event.
 
Share this answer
 
Comments
Vani Kulkarni 3-Jul-12 3:28am    
Nice ref! +5!
Prasad_Kulkarni 3-Jul-12 3:37am    
Thank you Vani!
There is no certain way to do this - it depends on the browser used, and what the user does - if you just turns off the power to his PC, or his internet connection fails then you will never get the "close browser" signal, and so the log off will never occur.

However, you can handle the browser onbeforeunload event (which should work in IE and FF) in the javascript, and signal it to the server. If they don't have javascript enabled though...

It would be better to redesign your system to work round it.
 
Share this answer
 
please Null the sessions in finally block.
Finally block always executed
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900