Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 3rd, 2004, 03:30 AM
Authorized User
 
Join Date: May 2004
Location: , , .
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.

Reply With Quote
  #2 (permalink)  
Old June 3rd, 2004, 03:40 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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.
Reply With Quote
  #3 (permalink)  
Old June 3rd, 2004, 04:35 AM
Authorized User
 
Join Date: May 2004
Location: , , .
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?

Reply With Quote
  #4 (permalink)  
Old June 3rd, 2004, 05:21 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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.
Reply With Quote
  #5 (permalink)  
Old June 3rd, 2004, 06:46 AM
Authorized User
 
Join Date: May 2004
Location: , , .
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

Reply With Quote
  #6 (permalink)  
Old June 3rd, 2004, 06:51 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
Reply With Quote
  #7 (permalink)  
Old June 3rd, 2004, 06:52 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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.
Reply With Quote
  #8 (permalink)  
Old June 3rd, 2004, 07:26 AM
Authorized User
 
Join Date: May 2004
Location: , , .
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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! :)

Reply With Quote
  #9 (permalink)  
Old June 3rd, 2004, 07:26 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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.
Reply With Quote
  #10 (permalink)  
Old June 3rd, 2004, 04:10 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reset Cache when Application_End thanhh83 .NET Framework 2.0 1 September 3rd, 2006 09:17 AM



All times are GMT -4. The time now is 01:34 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.