|
|
 |
| 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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

September 16th, 2007, 05:52 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Location: New York, NY, USA.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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 :)
|

September 17th, 2007, 12:18 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Location: New York, NY, USA.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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 :)
|

September 17th, 2007, 12:40 PM
|
|
Wrox Author
Points: 12,827, Level: 49 |
|
|
Join Date: Oct 2005
Location: Akron, Ohio, USA.
Posts: 4,029
Thanks: 1
Thanked 42 Times in 42 Posts
|
|
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
================================================== =========
|

September 17th, 2007, 12:47 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Location: New York, NY, USA.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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 :)
|

September 17th, 2007, 01:09 PM
|
|
Wrox Author
Points: 12,827, Level: 49 |
|
|
Join Date: Oct 2005
Location: Akron, Ohio, USA.
Posts: 4,029
Thanks: 1
Thanked 42 Times in 42 Posts
|
|
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
================================================== =========
|

September 17th, 2007, 03:38 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Location: New York, NY, USA.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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 :)
|

September 17th, 2007, 03:47 PM
|
|
Wrox Author
Points: 12,827, Level: 49 |
|
|
Join Date: Oct 2005
Location: Akron, Ohio, USA.
Posts: 4,029
Thanks: 1
Thanked 42 Times in 42 Posts
|
|
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
================================================== =========
|

November 3rd, 2009, 01:20 AM
|
|
Registered User
|
|
Join Date: Oct 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
ASP NET How to get a sessionid..
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |