Video Lesson 3: userCount

Rate It (1)

Last post 01-09-2007 4:03 AM by basiltabethacat. 9 replies.

Sort Posts:

  • Video Lesson 3: userCount

    08-18-2006, 6:45 AM
    • Member
      617 point Member
    • ThienZ
    • Member since 08-18-2006, 9:20 AM
    • Posts 124

    Hello,

    right now I'm learning ASP:NET from the videos from this site:

    http://www.asp.net/learn/videos/default.aspx?tabid=63

    I'm trying to do this userCount, it is counted in this method:

    void Session_Start(object sender, EventArgs e)

    {

    int userCount = int.Parse(Application.Get("userCount").ToString());

    userCount++;

    Application.Set(

    "userCount", userCount);

    }

    and it is decremented with a similar way in the method Session_End.

    In the file Default.aspx.cs I have this line in the method Page_Load:

    Page.Response.Write("Number of user: " + Application.Get("userCount").ToString());

     

    If now I run in debug mode, I can see the browser with "Number of user: 1"

    If I open another browser it says "Number of user: 2"

    The same for the third "Number of user: 3"

    If I refresh the first and second browser, now they say "Number of user: 3" too.

    But if I close a browser and refresh the others, they still have "Number of user: 3".... What should I do so the method Session_End can run?

    Second question: If I open a new browser with ctrl+n, the number of users do not increase. Does this way not count as a new session?

     

    Thx in advance.

     

     

  • wrong sub topic

    08-18-2006, 9:22 AM
    • Member
      617 point Member
    • ThienZ
    • Member since 08-18-2006, 9:20 AM
    • Posts 124
    Actually I created this topic in "Getting Started", I don't know why it landed in "State Management"... I couldn't find erase, move, nor edit... So sorry folks....
  • Re: Video Lesson 3: userCount

    08-20-2006, 10:36 PM
    • Contributor
      4,635 point Contributor
    • Gordon-Freeman
    • Member since 07-17-2006, 8:19 AM
    • SH, PRC
    • Posts 909

    Hi~ Firstly, only under InProc session mode will Session_End fire. Secondly, I found it's fired only when you have go through a entire request and has something stored in session state. Plz correct me if I was wrong~

    你好! Just FYI o_O
  • Re: wrong sub topic

    08-21-2006, 7:47 AM
    • All-Star
      30,697 point All-Star
    • StrongTypes
    • Member since 12-13-2005, 4:21 PM
    • California
    • Posts 6,007
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    ThienZ:
    Actually I created this topic in "Getting Started", I don't know why it landed in "State Management"... I couldn't find erase, move, nor edit... So sorry folks....

    It was moved here because your topic is in regards to the session state.

    Ryan

    Ryan Olshan
    ASPInsider | Microsoft MVP, ASP.NET
    http://ryanolshan.com

    How to ask a question
  • Re: Video Lesson 3: userCount

    08-21-2006, 7:55 AM
    • Member
      617 point Member
    • ThienZ
    • Member since 08-18-2006, 9:20 AM
    • Posts 124
    Gordon-Freeman:

    Hi~ Firstly, only under InProc session mode will Session_End fire. Secondly, I found it's fired only when you have go through a entire request and has something stored in session state. Plz correct me if I was wrong~

     

    hi, what do you mean with InProc? What should I do so I can get my counter right?

    Thx

  • Re: Video Lesson 3: userCount

    08-21-2006, 2:47 PM
    • Member
      352 point Member
    • FCsteve
    • Member since 03-30-2005, 10:07 AM
    • Posts 75
    Sounds like it is working fine. Sessions with the server don't end when you close the browser. Mainly because you cannot guarentee that a client will actually send a final response saying "bye.". So this is why the server uses session timeouts, when the client is issued a session, the cookie placed in the clients computer, and the server starts a timer (defualt is 20mins), if this timer hits zero, the session expires server side and the server can re-issue that session id again (if it wants). However, if the client comes back to the server (with postback etc) the timer is reset and starts counting down again..

    This is why your system does not count down when you close a browser instance, because the technical view of a session is not as simple as the relationship of the client browser being active on your site.
  • Re: Video Lesson 3: userCount

    08-21-2006, 10:53 PM
    • Contributor
      4,635 point Contributor
    • Gordon-Freeman
    • Member since 07-17-2006, 8:19 AM
    • SH, PRC
    • Posts 909

    Session-State Modes is here. Emm...and here is some one writing about Session_End. Seems unless you call Session.Abandon() or session timeout it never fires.

    I've seen some one make an ajax call to a specified page in 'onbeforeunload' of the form to explicited invoke Session.Abandon(), but that's much more complicated~

    你好! Just FYI o_O
  • Re: Video Lesson 3: userCount

    08-22-2006, 3:29 AM
    • Member
      617 point Member
    • ThienZ
    • Member since 08-18-2006, 9:20 AM
    • Posts 124
    Thank you guys! Now I understand why Session_End didn't fired
  • Re: Video Lesson 3: userCount

    01-09-2007, 3:51 AM

    Hi!

     I appear to have a similar problem to this post.  I am following (and reproducing in my own VWD) the Video Lesson 03 with regards to userCount and postback.  I am an absolute beginner (began VWD this morning - it is now 5pm Australia time).  The beginner videos I am using are from:

     http://msdn.microsoft.com/vstudio/express/vwd/learning/

    (different site from the the original message of this thread).

     

    My userCount also does not appear to work.  I have checked and rechecked my version with the video, but the outcome (in running/debugging) differs.  And the outcomes in IE7 and FireFox (both not appropriate) also differ from each other.  Here is the code I have used.

    Global.asax

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

    ' Code that runs on application startup

    Application(

    "userCount") = 0

    End Sub

     

    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)

    ' Code that runs on application shutdown

    End Sub

     

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

    ' Code that runs when an unhandled error occurs

    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)

    ' Code that runs when a new session is started

    Application(

    "userCount") += 1

    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)

    ' Code that runs when a session ends.

    ' Note: The Session_End event is raised only when the sessionstate mode

    ' is set to InProc in the Web.config file. If session mode is set to StateServer

    ' or SQLServer, the event is not raised.

    Application(

    "userCount") -= 1

    End Sub

    I added (as per the video) the initialisation value for Application_Start as 0

    I added the session count as incrementing by 1 in Session_Start

    I added the session count as decreasing by 1 in Session_End

    aspx.vb

    Protected

    Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Page.IsPostBack = False Then

    TextBox1.Text =

    ""

    End If

    Page.Response.Write(

    "User Count is: " & Application("userCount").ToString())

    End Sub

    I added (as per video) the second last line here to provide the count for me on running/debugging.

    Now...The code as per the video all makes sense to me, and it was tested on the video (to increment as a new browser was opened - he opened three - then refreshed the previous two, which reverted to show that 3 browsers were open).  This appeared fine on the video; however, when I attempted to do the same thing, MINE didn't work.  My default browser is FireFox (I love the tabbed browsing and IE7 appears to have bugs in terms of recognising the allowance of cookies!! and often tells me that a website is non-existant, whereas FireFox can always find it - therefore I flip between the two).  In debugging, FireFox is initiated, but regardless of the number of tabs or windows opened (I tried both), the userCount is 1 and does not increment.  Then, when I open IE7, (with several FireFox browsers still open) the count always starts at 2 regardless of the number of FireFox browser windows open.  When I open more tabs or windows (I tried both) in IE7 the numbers increment appropriately; however, when I go back to a previous IE7 tab and refresh it, the count continues to increment from the last browser open, rather than simply reflect the actual number of browsers open.  This is difficult to explain...in other words, if I have 3 IE7 browsers open (the last says "User Count is 3", then return to the first browser and refresh it the new message reads "User Count is 4" - even if only 3 windows were ever opened.  Now, that did not occur on the video.

     OK, I did notice the comment in the code stating that Session_End will only express if SessionStateMode is InProc.  I figured this as a possible reason but I had no idea what this meant, so I consulted the Help system, where it stated that InProc is the default for VWD.  Furthermore, it stated that InProc required StateServer mode with instructions for changing this, then "Set the mode attribute of the sessionState element to SQLServer", where "sessionState" leads to another set of instructions for changing this.  In fact this is the very page that you have referred the thread originator to (the other thread no longer exists).

    The thread originator appeared to be querying that the number of users did not DECREASE when browsers were closed and remaining ones refreshed.  This is NOT my problem - I also noticed this, of course, but my initial replay of the video enlighted me that HE didn't test the DECREASE of users either.  He simply says that we would have to wait for 20 minutes to see the decrease - that's fine.

    Now, I am an absolute beginnerEmbarrassed, so most of the HELP file was Double-Dutch to me.  I only described it in order to demonstrate my attempted at finding my own solutions in order to cancel out reiterations of what I HAD discovered (amongst the Double-Dutch).Smile

     Now, from my experiences, I can guess that the system sees 1 FireFox user regardless of actual FireFox users (and perhaps that is because all windows were opened on the one computer???).  But I would have thought that IE7 would have been without error or problem.  Also, if the Session_End system does not recognise the number of FireFox users, the number of users count isn't going to be very practical is it?  Lots and lots of people use FireFox.

    Could I have answers to:

    1. Do I have any misunderstandings or misconceptions evident in this post (remember, absolute beginner)?
    2. Is there an error or problem in the code?
    3. Why doesn't FireFox increment?
    4. Why does IE7 continue to increment inappropriately?
    5. Why do the two browsers react differently, anyway?
    6. Regarding your statement, "Seems unless you call Session.Abandon() or session timeout it never fires.", is this the answer?  If so, could you provide me with the exact code and tell me were I would place this?  Would this be an alternative to the userCount decrease code in Session_End?

     Please remember that I am an absolute beginner - hence, could I have a replay without jargonistic language.

     Thankyou in advance for any help,

     Fran...

  • Re: Video Lesson 3: userCount

    01-09-2007, 4:03 AM

    My appologies, I should have told you:

    • I am running XP Home sp2 (fully updated)  .Net Framework 2.0.
    • I have installed SQL Express and management, VWD Express, VB Express, C# Express, XNA Game Express and VS Express tools and samples etc.
    • I downloaded the .Net Framework 2.0 and the Express suite of programs several days ago
    • I have completed VB Express Videos up to Lesson09 (including the sorting out of the apparently universal databinding problem with respect to the two databases and the saving to one problem).
    • I have no programming or website creation experience whatsoever...until now. (hence the request to be *nice* to the confused and brain overloaded beginner.

    Thanks,

     Fran...

Page 1 of 1 (10 items)