Gilad Adar
Gilad Adar

Reputation: 167

Run sql query when a browser was closed

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.

Upvotes: 2

Views: 1632

Answers (4)

Gilad Adar
Gilad Adar

Reputation: 167

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.

Upvotes: 0

Daniel Björk
Daniel Björk

Reputation: 2507

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

Upvotes: 1

JensB
JensB

Reputation: 6880

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.

Upvotes: 1

Amadan
Amadan

Reputation: 198446

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.

Upvotes: 2

Related Questions