I tried with the following code from the second link you provided but the code behind function is not getting invoked.
This is the code on aspx page
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        function doUnload() {
            $.ajax({
                type: "POST",
                url: "saveLeavingTime.aspx/InsertLogOutTime",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
                    alert(r.d);
                },
                error: function (r) {
                    alert(r.responseText);
                },
                failure: function (r) {
                    alert(r.responseText);
                }
            });
        }
    </script>
and this one in the code behind file
 
protected void InsertLogOutTime()
    {
        string ip = "";
        string AT = "";
        if (Session["ip"] != "" || Session["ip"] != null)
        {
            ip = Session["ip"].ToString();
        }
        else
        {
            ip = "N/A";
        }
        if (Session["arrivalTime"] != "" || Session["arrivalTime"] != null)
        {
            AT = Session["arrivalTime"].ToString();
        }
        else
        {
            AT = "N/A";
        }
       
        string constr = ConfigurationManager.ConnectionStrings["CS"].ConnectionString;
        string sqlStatment = string.Format(@"INSERT INTO [dbo].[VISIT_DURATION]([IP_ADDRESS]
                   ,[LEAVING_TIME]
                   ,[ARRIVAL_TIME]) VALUES(@ip,@LeavingTime,@ArrivalTime)");
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
            {
                con.Open();
                cmd.Parameters.AddWithValue("@ip", ip.ToString());
                cmd.Parameters.AddWithValue("@LeavingTime", DateTime.Now.ToLongTimeString());
                cmd.Parameters.AddWithValue("@ArrivalTime", AT.ToString());
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }
 
http://www.aspforums.net/Threads/408364/Solved-Save-Insert-Logout-Time-on-Browser-Close-in-ASPNet/ 
The above link worked but unfortunately, it did not solve my purpose. I want to capture the time when the user closes tab/browser or leaves the current page.
I tried with Application_End and Session_End events in the Global.asax file but it didn't work too.