Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional 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
 
Old September 16th, 2007, 04:52 PM
Authorized User
 
Join Date: Jun 2003
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Global.Asax Session_End and Session.SessionID

Hello,

I am working on a website, and my next task is to track the number of online users.

Decided the best approach to do this would be to add a strSessionID field to my customers table. The idea is to populate it with Session.SessionID at login, and then clear it out at Session_End.

Well, it seems I can put the value into my table when a user logs in, but something isn't working in my Session_End event.

Can anyone help?

Here is the code in my Global.asax:
**************************************************
<%@ Application Language="VB" Inherits="myApp.Application.GlobalAsax" %>

<%@Import NameSpace="myApp.Data" %>

<script language="VB" runat="server">

Sub Application_Start(Sender As Object, E As EventArgs)
  ' Application startup code goes here
End Sub

Sub Application_End(Sender As Object, E As EventArgs)
  ' Clean up application resources here
End Sub

Sub Session_Start(Sender As Object, E As EventArgs)
  'Session startup code goes here.
End Sub

Sub Session_End(ByVal Sender as Object, ByVal E as EventArgs)
    '' Clear out the matching strSessionID field in tblCustomers.

   Dim strSessionID As String = Session.SessionID.ToString().Trim()


   Dim sql As String = "UPDATE tblCustomers SET strSessionID = NULL WHERE strSessionID = '" & strSessionID & "'"

   myDataHandler.runSql(sql)


End Sub


************************************************

Thank you in advance to anyone who can help shed some light here!



Susan :)
__________________
Susan :)
 
Old September 17th, 2007, 11:18 AM
Authorized User
 
Join Date: Jun 2003
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi everyone,

Just wanted to let you know that I have solved this little puzzle.

I suspected that the SessionID was changing somehow, therefore, my query wasn't updating, because it wasn't finding a match on SessionID. I have not actually PROVED this, because i couldn't find a way to generate some instrumentation to tell me what the SessionID was at the point of Session_OnEnd. (Oh, I suppose, I could have updated a field in my table with what I suspect was the "new" value, if I really really wanted. But my head was elsewhere yesterday, I guess!)

But ANYWAY, all I did was change my query to find a match on idCustomer, instead of strSessionID.

So now, Session_End looks like this:
**************************************************

Sub Session_End(ByVal Sender as Object, ByVal E as EventArgs)
    '' Clear out the strSession_LoggedOn and dtmSession_LoggedOn fields in tblCustomers.

   Dim iCustomerID As Integer = MySessionHandler.GetCustomerID(Context)

   Dim sql As String = "UPDATE tblCustomers SET " & _
                       "strSessionID_LoggedOn = NULL, " & _
                       "dtmSessionID_LoggedOn = NULL " & _
                       "WHERE idCustomer = " & iCustomerID

   myDataHandler.runSql(sql)


End Sub

**************************************************

So the moral of the story is, do NOT rely on Session.SessionID.

As an alternative, based on other articles I read, I actually wrote code in my Master file that checked the timestamp on dtmSessionID_LoggedOn. If a user was logged on and making server calls, then at the page load event for the Master, the date for that user would get refreshed. After that, there was code to check to see how many dtmSessionID_LoggedOn records had an elapsed time of > 20 minutes; then it would null out the session fields in my table.

However, we decided NOT to go that route, as we are trying to minimize lots of database calls within our pages.

So then it was back to the Session_OnEnd event ...glad to say that all I had to do was to veer away from using the SessionID value as a matcher-upper, and it worked fine!

Hope this helps anyone else out there who has been struggling with this issue.

Best,


Susan :)
 
Old September 17th, 2007, 11:40 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

It is also worth pointing out that you should never rely on the Session_OnEnd event to preform some business critical task since you cannot gurantee that the Session_OnEnd event will fire 100% of the time.

Very nice example none the less susan ^^

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Professional Search Engine Optimization with ASP.NET

Professional IIS 7 and ASP.NET Integrated Programming

================================================== =========
 
Old September 17th, 2007, 11:47 AM
Authorized User
 
Join Date: Jun 2003
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ARRRGGGH!!!!

Actually, it DID NOT work. Durr. I failed to take out the code that updated my table on a user's action of clicking a LOG OUT button.

No, this is STILL not firing. Does not fire when user logs out, does not fire when user idles out, does not fire AT ALL.

People?? What is UP with Session_OnEnd?

Susan :)
 
Old September 17th, 2007, 12:09 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Well, as I said, Session_OnEnd is not guranteed to fire so you need to take this into account when placing code in that event.

If you use SQL Server to store your session state, Session_OnEnd will NEVER fire and, I believe, the same holds true if you are using a StateServer. With that said, the only Mode that supports OnEnd is InProc.

If, when the user logs out, you are not calling Session.Abandon session_onend will not be called since you have not explicitly ended it.


This doc may be of some help to you: http://forums.asp.net/p/7504/7504.aspx

hth




================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Professional Search Engine Optimization with ASP.NET

Professional IIS 7 and ASP.NET Integrated Programming

================================================== =========
 
Old September 17th, 2007, 02:38 PM
Authorized User
 
Join Date: Jun 2003
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you so much for that reply. We decided to re-implement the code I wrote, that uses a date/time stamp. It's simple, it's reliable, and it works!


So, stupid question, why have Session_OnEnd events at all if they are not guaranteed to trigger? Is this something else I have to go bug Bill Gates about?! SHEEESH :).

Many thanks,


Susan :)
 
Old September 17th, 2007, 02:47 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Because the event still fires and it still occurs, albeit conditional. When using InProc Session State management Session_OnEnd should fire when Session.Abandon() is called or the Session Times out *BUT* if the Worker process crashes or IIS/the Server is restarted this event will NOT be called.

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Professional Search Engine Optimization with ASP.NET

Professional IIS 7 and ASP.NET Integrated Programming

================================================== =========
 
Old November 3rd, 2009, 01:20 AM
Registered User
 
Join Date: Oct 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default ASP NET How to get a sessionid..

Hai...

Here is the solution....


http://muruganad.com/ASP.NET/ASP-NET...sessionid.html


Thanks!

Murugan Andezuthu Dharmaratnam

http://muruganad.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to declare the global variable in global.asax? calyn_gately ASP.NET 3.5 Basics 0 August 6th, 2008 08:06 PM
session.sessionid same for all items sarah lee ASP.NET 1.0 and 1.1 Basics 1 October 31st, 2006 12:40 PM
Session States, Global. asax and web.config ohmbhu General .NET 2 July 1st, 2004 09:15 AM





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