I posted earlier on this site for help on where to set a command when a user session timesout. I posted this in the global.asax file:
Imports System.Web
Imports System.Web.SessionState
Imports System.Diagnostics
Public Class Global Inherits System.Web.HttpApplication
Sub Application_Start(ByVal Sender As Object, ByVal E As EventArgs)
Application("CurrentUsers") = 0
Application("TotalSessions") = 0
Application("TotalSessionsStartDate") = DateTime.Now.ToString("d")
Application("TotalHits") = 0
Application("TotalHitsStartDate") = DateTime.Now.ToString("d")
End Sub
Sub Application_BeginRequest(ByVal Sender As Object, ByVal E As EventArgs)
Application("TotalHits") += 1
End Sub
Sub Session_Start(ByVal Sender As Object, ByVal E As EventArgs)
Application("CurrentUsers") += 1
Application("TotalSessions") += 1
End Sub
Sub Session_End(ByVal Sender As Object, ByVal E As EventArgs)
Application("CurrentUsers") -= 1
Dim conLogout As New OdbcConnection( System.Configuration.ConfigurationManager.AppSettings.Get("ConnectionString") )
Dim cmdUpdate2 As New OdbcCommand( "UPDATE ProfileComments SET WasRead=1 WHERE ReceiveUserID=" & Session("UAID") & " AND WasRead=0", conLogout )
Dim cmdUpdate3 As New OdbcCommand( "UPDATE Messages SET WasRead=1 WHERE ReceiveUserID=" & Session("UAID") & " AND WasRead=0", conLogout )
conLogout.Open()
cmdUpdate.ExecuteNonQuery()
cmdUpdate2.ExecuteNonQuery()
cmdUpdate3.ExecuteNonQuery()
conLogout.Close()
Session.Contents.RemoveAll()
Session.Abandon()
End Sub
Sub Application_End(ByVal Sender As Object, ByVal E As EventArgs)
Application("CurrentUsers") = Nothing
End Sub
End Class
Two things:
1. It has never.. ever done the commands it was supposed to. Even when waiting for 2-3 days for a session to timeout, where the timeout is at 20 minutes on the web.config file.
2. I cannot seem to combine all three queries into one. Any ideas on that part? or even condense into two?
I am looking for a way to make a user appear offline when they timeout or logout or close the browser (which deals with timeout). Any help?