Results 1 to 5 of 5

Thread: Session_End and Page_unload?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Question Session_End and Page_unload?

    I have a strange problem. I update a flag in my database when a user is logging in.
    Code:
    Sub ConnectUser(ByVal userName As String)
            strUserCn = "Provider=Microsoft.jet.oledb.4.0;Data Source=" & Server.MapPath("login.MDB") & ";"
            Dim strSql As String = "update Person SET Online='Yes', LastLogin='" & CDate(Date.Now) & _
            "', Logins=" & CInt(Session("NoOfLogins") + 1) & " WHERE UserName='" & userName & "'"
            Dim objConnection As New OleDbConnection(strUserCn)
            Dim objCommand As New OleDbCommand(strSql, objConnection)
            Dim objDataReader As OleDbDataReader
    
            objConnection.Open()
            objCommand = New OleDbCommand(strSql, objConnection)
            objCommand.ExecuteNonQuery()
            objConnection.Close()
        End Sub
    This works fine, the flag is updated and the value is 'Yes'.
    But then I implement this code, for setting the value to 'No', when the user terminates the session:
    Code:
    Private Sub Page_Unload(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Unload
            clsUserLogin1.DisconnectUser(Session("username"))
        End Sub
    where
    Code:
    Sub DisconnectUser(ByVal userName As String)
            strUserCn = "Provider=Microsoft.jet.oledb.4.0;Data Source=" & Server.MapPath("login.MDB") & ";"
            Dim strSql As String = "update Person SET Online='No' WHERE UserName='" & userName & "'"
            Dim objConnection As New OleDbConnection(strUserCn)
            Dim objCommand As New OleDbCommand(strSql, objConnection)
            Dim objDataReader As OleDbDataReader
    
            objConnection.Open()
            objCommand = New OleDbCommand(strSql, objConnection)
            objCommand.ExecuteNonQuery()
            objConnection.Close()
        End Sub
    When I have logged in and investigates the value in the database I see that the value is 'No' right after I have loaded the application??? What is happening? I know that I could make a "Logout" button, but it would be great if this could work.

  2. #2
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    The best place to put this code would be in your Session_Start and Session_End events of your global file in the root.

    Parksie

  3. #3
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    Originally posted by venerable bede
    The best place to put this code would be in your Session_Start and Session_End events of your global file in the root.
    Yep. Page_unload is just part
    of the page life cycle and will get called everytime a request is made, hence why your flag is always set to no when you check it.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    Great, thanks I will try that.

  5. #5
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    Originally posted by Fishcake
    Yep. Page_unload is just part
    of the page life cycle and will get called everytime a request is made, hence why your flag is always set to no when you check it.
    My God fishcake !

    Did I actually offer good advice for a change ?

    Parksie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width