 |
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application . |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

June 3rd, 2004, 03:30 AM
|
Authorized User
|
|
Join Date: May 2004
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Application_End HELP!
Hi,
I am trying to write some code in global.asax file to track the logout time when the user quits the application. Here is the code in global.asax. vb file
Code:
Imports System.Web
Imports System.Web.SessionState
Imports System.Data
Imports System.Data.OLEDB
Public Class Global
Inherits System.Web.HttpApplication
Private Shared db As String = ConfigurationSettings.AppSettings("db")
Protected connectionString As String = ConfigurationSettings.AppSettings("connectionString1")
#Region " Component Designer Generated Code "
Public Sub New()
MyBase.New()
'This call is required by the Component Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
End Sub
#End Region
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session is started
End Sub
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires at the beginning of each request
End Sub
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires upon attempting to authenticate the use
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Fires when an error occurs
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
Dim objCon As New OleDbConnection(connectionString)
objCon.Open()
Dim objCommand As OleDbCommand
Dim objDataReader As OleDbDataReader
Dim strSQL As String
Session("logoutTime") = DateTime.Now().ToString("T")
strSQL = "update Emp_Login " _
& "set logout_time = '" & Session("LogoutTime") & "' " _
& "where login_time = '" & Session("LoginTime") & "' and emp_no = '" & Session("userSession") & "'"
objCommand = New OleDbCommand(strSQL, objCon)
objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)
objDataReader.Close()
objCon.Close()
Session.Contents.RemoveAll()
End Sub
End Class
The database does not store or execute my SQL query when I quit my browser.
Any help is greatly appreciated.
|

June 3rd, 2004, 03:40 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
There are a few problems with your approach. First of all, Application_End doesn't fire when your users close their browsers; it fires when the entire application is ended normally; e.g. through a restart of IIS or a reboot of the machine. This means that Application_End does not apply to a specific user.
So you should actually code your logic in Session_End, which fires when a user's session ends. This event will fire for each user of your application. However, it doesn't fire when the user closes the browser, but when the session times out or is ended explicitly. When the user closes the browser, there is no communication between the browser and the server, so the server doesn't know the session can be ended. Take a look here for a more thorough explanation: http://www.asp101.com/articles/john/...nd/default.asp
Also, be aware that Session_End does not fire in all circumstances. Only when you store session state InProc this event will fire. It won't work when you store session state in SQL Server for example.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|

June 3rd, 2004, 04:35 AM
|
Authorized User
|
|
Join Date: May 2004
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Imar.
Quote:
quote:but when the session times out or is ended explicitly
|
1. What do you mean by ended explicitly?
2. Is it possible to run a procedure which I have put into my Application_End in to a method on my page which executes when the browser is closed for THAT page .....perhaps onunload javascript or something
Any ideas anyone?
|

June 3rd, 2004, 05:21 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi again,
You can explicitly end a session through code using the Abandon method of the Session object:
http://msdn.microsoft.com/library/de...andonTopic.asp
This will trigger the Session_End event (in the circumstances I described earlier). You could call this method in a Logout.aspx page, for example that is loaded when a user clicks a Logout button.
You could indeed call a page from within the unload event of the browser window. That page could then do what your Application_End does now. But even then it's not guaranteed that the page will run. If, for example, a user with a dial up connection disconnect from the ISP, there will be no connection when the browser is closed, so your unload page will not be called. So, you should use Session_End and other "unload methods" only for things that are not 100% required to run.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|

June 3rd, 2004, 06:46 AM
|
Authorized User
|
|
Join Date: May 2004
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ok Thanks. I will try and work on the onunload bit. But I don't know how to go about it though.
The following is my code in VB which I want to execute
Code:
Sub Some_Function
Dim objCon As New OleDbConnection(connectionString)
objCon.Open()
Dim objCommand As OleDbCommand
Dim objDataReader As OleDbDataReader
Dim strSQL As String
Session("logoutTime") = DateTime.Now().ToString("T")
strSQL = "update Emp_Login " _
& "set logout_time = '" & Session("LogoutTime") & "' " _
& "where login_time = '" & Session("LoginTime") & "' and emp_no = '" & Session("userSession") & "'"
objCommand = New OleDbCommand(strSQL, objCon)
objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)
objDataReader.Close()
objCon.Close()
End Sub
Now the onunload will be something like this
<html>
<body onunload = "some function()">
</body>
</html>
I have a few problems
1. My "some function()" is in VB. How can I get JavaScript to call it?
2. Is there a VB version of onunload? So I can avoid JavaScript altogether and execute my "some function() " in VB
3. Thirdly, suppose there is a way for javascript to execute the VB code, will it work as I have to put the following statements at the top of the page and I doubt whether these will be recognized within the Sub Function.
Code:
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OLEDB" %>
<%@ Import Namespace="System.Data.SqlClient" %>
Appreciate anybody who knows how to code this or give me a headstart.
|

June 3rd, 2004, 06:51 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
The onload could call a new ASPX page at the server with client side JavaScript. The Page_Load of that new page could then update the database at the server.
E.g.:
<body onunload="location.href='MySessionOnEnd.aspx';">
Then MySessionOnEnd.aspx is like any other ASPX page, and updates the database.....
Imar
|

June 3rd, 2004, 06:52 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Also look at the section titled "Update: Javascript Session Ender" in the article at www.asp101.com I pointed you to.
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|

June 3rd, 2004, 07:26 AM
|
Authorized User
|
|
Join Date: May 2004
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hey Imar,
I already had a logout page. Thanks for the code
Quote:
quote:<body onunload="location.href='MySessionOnEnd.aspx';">
|
It finally works now. You made my day! :)
|

June 3rd, 2004, 07:26 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Doesn't onUnLoad also fire when you move from one page to another? Or does it only fire when the browser is closed? I seemed to recall that onUnLoad fires any time the browser document is unloaded, either by going to a different page, or closing the browser.
|

June 3rd, 2004, 04:10 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Yes, it does. So, effectively, you'll log out with each link to the next page.
One way to avoid this problem is to use a frame or an iframe. Add the onunload to the outer frame, so it will only fire when the browser is closed, or the user types in a new link. Within the frameset (consisting of exactly one frame with a 100% width and height) you can host your app. Navigating to other pages will cause the inner onunload to fire, but since there is no handler attached, no harm is done.
Not a pretty solution, but IMO getting Session_End to fire never is.... Personally, I do not depend on that event.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|
 |