[Solved] Save (Insert) Logout Time on Browser Close in ASP.Net

on Jan 11, 2015 10:52 PM

Friends, i want to insert logout time in database when user close browser. i have tried unload/ onbeforeunload in javascript but i doesn't work with all browser like IE (both, tab close & close button) an Firefox (close button). In my javascript code i called PageMethods with in onbeforeunload event, which insert data into database. Its working good with Opera and Chrome but not on the above mentioned browser. Is it possible in this way. Plz suggest me the suitable solution. I am not very much familiar with jQuery. I want suggestion in Javascript.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Jan 12, 2015 08:15 AM

Using the Session_End event in the Global.asax file.

You need to set the Inproc mode in the sessionState so that we can make use of Session_End event. For testing purpose i have set 1 min as the Session timeout time.


CREATE TABLE [dbo].[UserSessionEndTime](
	[UserId] [int] NOT NULL,
	[TimeOutTime] [datetime] NULL



    <sessionState mode="InProc" timeout="1"/>
    <compilation debug="true" targetFramework="4.0"/>

At the login page Set the Session with the User's Id. Here its in integer format.


protected void Login(object sender, EventArgs e)
    // Write some code to authenticate the user.
    // Set the Id of User’s table in Session.
    //It’s for testing purpose i have set it to 1.
    Session["UserName"] = 1;


<%@ Application Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script RunAt="server">
    void Session_End(object sender, EventArgs e)
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        string sqlStatment = "INSERT INTO UserSessionEndTime VALUES(@UserId,@TimeOutTime)";
        using (SqlConnection con = new SqlConnection(constr))
            using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
                cmd.Parameters.AddWithValue("@UserId", Convert.ToInt32(Session["UserName"]));
                cmd.Parameters.AddWithValue("@TimeOutTime", DateTime.Now);


Protected Sub Login(ByVal sender As Object, ByVal e As EventArgs)
    ' Write some code to authenticate the user.
    ' Set the Id of User’s table in Session.
    ' It’s for testing purpose i have set it to 1.
    Session("UserName") = 1
End Sub


<%@ Application Language="VB" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
    Private Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Dim sqlStatment As String = "INSERT INTO UserSessionEndTime VALUES(@UserId,@TimeOutTime)"
        Using con As SqlConnection = New SqlConnection(constr)
            Using cmd As SqlCommand = New SqlCommand(sqlStatment, con)
                cmd.Parameters.AddWithValue("@UserId", Convert.ToInt32(Session("UserName")))
                cmd.Parameters.AddWithValue("@TimeOutTime", DateTime.Now)
            End Using
        End Using
    End Sub