2

How can i run sql query when the browser was closed by a user in asp.net c# ?

if (browser was closed){
SqlCommand cmd = new SqlCommand("check_if_closed", con);
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@user_id", txtUname.Text);
                        con.Open();
                        cmd.ExecuteNonQuery();
}

thanks.

Gk_999
  • 508
  • 8
  • 29
Gilad Adar
  • 167
  • 1
  • 14

4 Answers4

2

It's like asking someone to be sure to phone you in case they die. Can't be done. Web does not work that way. The best you can do is keep in touch with them regularly, and if they fail to check in assume they're dead.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • well there is always the `beforeunload` that is not 100% safe that can work, but you can use it. Its like place a program on the cell phone that check if he drops from the user hands to send you a message. And assume that when is die the phone will drop down. – Aristos May 22 '14 at 06:29
  • @Aristos: Actually, more like if the phone leaves the user's hand, for any reason. – Amadan May 22 '14 at 07:11
  • The same way the `beforeunloca` works, because is trigger when the user leaves the page, and go to some other. – Aristos May 22 '14 at 08:04
1

The best you can do it to capture the event of someone leaving your page. This will be fired by a browser going to a new page or closing the browser.

This can then call a specific url in which you can register the event on your server. I'm not sure all browsers will accept this event, and there is also a risk of the browser moving on if it takes to long.

Intercept page exit event

 window.onbeforeunload = function (e) {
      var message = "Your confirmation message goes here.",
      e = e || window.event;
      // For IE and Firefox
      if (e) {
        e.returnValue = message;
      }

      // For Safari
      return message;
    };

Another option, which probably works better, is that your javascript code "pings" your server every so often to confirm that the user is still on the page, you can then deduce that the user has left the page if no ping has been received after the ping interval has passed.

More info can also be found here: Capture event onclose browser

If I were to develop this I would go with pinging the server every 10-30 seconds and updating the user table with a "latest date the user was online" value.

Community
  • 1
  • 1
JensB
  • 6,663
  • 2
  • 55
  • 94
1

To be on the safe side you should put it in global.asax, but this will not be called directly on browser close but you can be sure it will be called. It will be called first when the session timeout or when its abandoned.

protected void Session_End(object sender, EventArgs e)
{
    SqlCommand cmd = new SqlCommand("check_if_closed", con);
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@user_id", txtUname.Text);
                        con.Open();
                        cmd.ExecuteNonQuery();
}

And to set the session timeout you put this in the web.config under the tag. Note that the timeout time is in minutes.

<sessionState mode="InProc" cookieless="true" timeout="1"/>

More info: http://msdn.microsoft.com/en-us/library/vstudio/ms178583(v=vs.100).aspx

Daniel Björk
  • 2,475
  • 1
  • 19
  • 26
  • http://stackoverflow.com/a/14376837/937131 Session_End will not fire unless you have created a session by setting data to it. – JensB May 22 '14 at 09:23
  • You also have to handle the case when the application pool is restarted, times out, or shuts down by calling your code in `Application_End` – JensB May 22 '14 at 11:18
  • Well you can handle that in the Application_End event. =) – Daniel Björk May 22 '14 at 11:20
0

So i have tried few thinks as you guys wrote but i managed to solved this issue with checking if the users cookie is alive or not by checking it every few minutes.

thanks anyway. hoping that i gave some ideas to others.

Gilad Adar
  • 167
  • 1
  • 14