0

I want to delete a record from data base as a user close the browser. the record which i want to delete is belong to the user who close the browser or log out. I write this code in global but it doesn't work. Can any body help me. Note: I want to delete the record that has username of log outed user. thank you very much the global code:

 void Session_End(object sender, EventArgs e) 
    {


        string connStr = ConfigurationManager.ConnectionStrings["dbconn"].ConnectionString;
        SqlConnection sqlconn = new SqlConnection(connStr);
        SqlCommand sqlcmd = new SqlCommand("delete ChatRoomList where UserName=@UserName", sqlconn);
        sqlcmd.Parameters.AddWithValue("@UserName", (string)Session["User"]);
        sqlconn.Open();
        sqlcmd.ExecuteNonQuery();
        sqlconn.Close();

    }
  • there is some java-script code which works on IE only. I think it would be better if you fire the delete statement when user log again. – शेखर Jan 08 '13 at 10:55
  • Thank you for replying.But i need something that works in every browser. Is there any way to figure out which username is in the special page or whole site? –  Jan 08 '13 at 10:58
  • you can go through this link (http://stackoverflow.com/questions/7255649/window-onbeforeunload-not-working) [http://stackoverflow.com/questions/7255649/window-onbeforeunload-not-working] – शेखर Jan 08 '13 at 11:49

2 Answers2

1

There is no reliable way to achieve this. Session_End event would only fire for in-process session state storage and the trigger point would be session expiration which is different than the browser close or user log-out.
So in your case, you may observe the event when your session actually gets timed out (if you have a logout link then you can force session expiry using Session.Abandon)

There are some other ways such as making an AJAX call to server to tell that user has logged out (or closing the browser window) and they may provide you with better results but again not 100% reliable. The most reliable way that I can think of is to have your own timer - i.e. to ping the server (using AJAX call) periodically from browser side and when pings are not received within certain time-out value, assume the user session to be dead/ended.

VinayC
  • 47,395
  • 5
  • 59
  • 72
  • Thank you for replying. Is there any way to figure out which username is in the special page or whole site? –  Jan 08 '13 at 10:56
  • @SaeedTalaee, I am sorry but I don't understand what you are asking. – VinayC Jan 08 '13 at 11:00
  • Thank you can you give me a reference or a guide line to search a bout it? –  Jan 08 '13 at 11:01
  • No problem.the story is this, as my users log in to my site, they fill a session that contains their username, and because of the username is individual, I use it in my programming every where. Now i make chat room for my site, and this chat room has a list that shows the users which exist in the chat room.What i want to do is to fill this list. I mean i want a list that shows which user is in chat room and which is not. that is it. –  Jan 08 '13 at 11:07
  • @SaeedTalaee, I will go for ping based solutions for chat application. Unfortunately, I don't have any example code etc. BTW, have you considered using alternatives such as SignalR - they would be ideal fit for chat application. Do have a look at http://signalr.net/ – VinayC Jan 08 '13 at 11:13
0

It will work on IE

   window.onbeforeunload = function (event) {
    if (((window.event.clientX || event.clientX) < 0) || ((window.event.clientY || event.clientY) < 0)) // close button
    {
        //call here you you jQuery function to delete either page method, web service or http 
    }
    if ((window.event.clientX < 0) || (window.event.clientY < 0)) // close button
    {
        //call here you you jQuery function to delete either page method, web service or http 

    }
    else if (window.event.altKey == true || window.event.ctrlKey == true) // ALT + F4
    {
       //call here you you jQuery function to delete either page method, web service or http handler
    }
    else // for all other unload events
    {

    }

}
});
शेखर
  • 17,412
  • 13
  • 61
  • 117
  • I am looking for the same and got the above :) – शेखर Jan 08 '13 at 11:08
  • thank you man.But most of my users use fire fox and chorme:( the story is this, as my users log in to my site, they fill a session that contains their username, and because of the username is individual, I use it in my programming every where. Now i make chat room for my site, and this chat room has a list that shows the users which exist in the chat room.What i want to do is to fill this list. I mean i want a list that shows which user is in chat room and which is not. that is it. Do have any idea? –  Jan 08 '13 at 11:13
  • I don't know the solution. But I can give you some idea try call a jQuery-Ajax call after a certain duration and update the list of active-users and show only them. – शेखर Jan 08 '13 at 11:23