keeping track of users who are online in a forum...vb .net

Hi guys,

I'm currently trying to create a real time list of members who are logged into our forums.

I'm currently trying to use Application variables so it's server side.

I have this in my global.asax:
Dim onlineUsers = New ArrayList
Application("OnlineUsers") = onlineUsers
'Application.Add("OnlineUsers", onlineUsers)

I also have this to invoke the addition of users to the list:

Dim userObj = New UserOnlineOffline(dsUser.Tables(0).Rows(0).Item("iUserID"), dsUser.Tables(0).Rows(0).Item("iClientID")) 'sets the SG userID and also the client ID

Application.Lock() 'lock site for application update
Application("OnlineUsers").CopyTo(tempList) 'temporarily save the application array list "OnlineUsers"

tempList.Add(userObj) 'add to the list for newest logged in member
Application("OnlineUsers") = tempList 'update old array list
Application.UnLock() 'remove lock

And finally I have a function which checks for online users and displays a small icon indicating they are online:

        Dim setHTMLStatus As New System.Text.StringBuilder
        Dim userObj As New UserOnlineOffline(iUserID, 101)
        Dim tempList As ArrayList

        tempList = Application.Item("UsersOnline")

        If (tempList.Contains(userObj)) Then 'if user has been online within the last 30 minutes...the stored procedure returns only one row with one column called "IsOnline" and the value is either 1 or 0
            setHTMLStatus.Append("<strong><a class=""online"" href=""/support/personalprofile.aspx?iUserID=" & iUserID & """") 'the styles for this anchor link can be found in "support/viewmessages.aspx"
            setHTMLStatus.Append("<font color=""#228B22"">" & chNickname & "</font></a></strong>")
            setHTMLStatus.Append("<strong style=""color:#228B22"">  (Online) </strong>")
            setHTMLStatus.Append("<strong><a href=""/support/personalprofile.aspx?iUserID=" & iUserID & """")
            setHTMLStatus.Append("<font color=""Grey"">" & chNickname & "</font></a></strong>")

        End If


I keep getting a Null error and I'm assuming my tempList is empty and I'm not copying correctly.

I tried:


but that doesn't work...

is there something wrong with the way i'm setting my application variable (at the very top of this post)?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alfred A.Commented:
Where is tempList declared based on this "Application("OnlineUsers").CopyTo(tempList)"?

And in your declaration, did you have this "Dim tempList As ArrayList" or this "Dim tempList As New ArrayList"?  It should be the second one.
lino_evolutionAuthor Commented:
Hi Alfred,

Thank you for your time and input! I really appreciate it!

I have it in global.asax

Do I need to lock the application under the heading Application_Start in global? Should I be declaring this somewhere else?

Thank you so much!
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        strConnection = System.Configuration.ConfigurationManager.ConnectionStrings("strConnection").ToString
        cmsConnection = System.Configuration.ConfigurationManager.ConnectionStrings("cmsConnection").ToString
        vccCentralConnStr = System.Configuration.ConfigurationManager.ConnectionStrings("vccCentralConnectionString").ToString
		CuteIMConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("CuteIMConnectionString").ToString
		'WebsiteName = System.Configuration.ConfigurationManager.AppSettings("sitename").ToString
        strRequiredCode1 = "* Required"
        Application("ActiveUsers") = 0
        Application.Add("OnlineUsers", New ArrayList)
    End Sub

Open in new window

Alfred A.Commented:
Yes.  It is better if Lock and then Unlock to protect the thread from interruption and to have an accurate visit count if you are using a counter.

Check this out:

Also, I can only see a New ArrayList in there.  Where is tempList?  if tempList is not instantiated, then you will get errors.  Is it set as "Dim tempList As New ArrayList" in the global.asax?   What I can see in your code is "Dim onlineUsers = New ArrayList" but no tempList.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

lino_evolutionAuthor Commented:
my apologies alfred...i somehow misread your previous reply.

Yes you are right...i completely forgot to set as new array and had it as [Dim tempList As ArrayList]

i will try this!

thanks again for your help i'll let you know how it goes!
lino_evolutionAuthor Commented:
so I've tried the instantiation but for some reason i still get a null to my "tempList" ...i've noticed that my Application("UsersOnline") is also i inserting / declaring this properly?
Alfred A.Commented:
You have to check if Application("UsersOnline") is null or empty before doing a CopyTo.  This is assuming that tempList is instantiated already.  This means that Application("UsersOnline") has no value at the point of CopyTo.

If (Not Application("UsersOnline") Is Nothing) AndAlso Application("UsersOnline") <> string.empty Then
   'Do the copyto
End if
lino_evolutionAuthor Commented:
I rearranged how I set the application variable and casted it as an arraylist (even tho the application variable "OnlineUsers" has an arraylist associated with it


Dim userObj = New UserOnlineOffline(dsUser.Tables(0).Rows(0).Item("iUserID"), 15) 'sets the SG userID and also the client ID

Application.Lock() 'lock site for application update

CType(Application("OnlineUsers"), ArrayList).Add(userObj)  'add to the list for newest logged in member
Application.UnLock() 'remove lock


Then I have this to check the application object arraylist:

        If (CType(Application("OnlineUsers"), ArrayList).Contains(userObj)) Then


now the list is for sure not empty...but for some reason I can't find a match for the user...kinda weird...

do you think i can add to the list by the type casting I have above?
Alfred A.Commented:
You can do a quick test by adding a dummy value to your userObj and then try to see if it matches.  I am not sure if you are dealing with AnonymousID and Profile.  AnonymousID is blank if User is authenticated and AnonymousID exists if user is anonymous/guest and user profile will not be available.
lino_evolutionAuthor Commented:
thank you so much alfred for all your help! i really appreciate it!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.