Public variables in a module - from 2 users affect each other

  • Thread starter Thread starter Floris van Haaster
  • Start date Start date
F

Floris van Haaster

Hi All!

I have a question, i have a web application and I store some member
information in a variable i declared in a module like:

Public some_info_variable as string
in module1.vb

But when another user comes to my website he is getting the same values from
the variable!!!
And i want the variable to be filled/read unique for each session/user.

I found this:
--------------------------------------------------
Using a module may be your problem. Depending on what kind of app (from
list above) it is. It also might not be. Modules may give you problems in
ASP.NET apps for example, as module level variables are shared across the
entire app. Each session would see the exact same variable. If each company
is using the same web server...
--------------------------------------------------

i found it @:
http://groups.google.nl/group/micro...the+same+web+server...&hl=nl#95b4ace6fa68c8c1

But what is the correct way to declare my variable i need in all my pages
but only for 1 user?!?!!

Thanks in advance!

Best regards

Floris
 
Store the variable in Session("VariableName") as this is unique to each
session. For global variables, store int Application("VariableName") and for
controls/page class level variable to persist, store them in
Viewstate("VariableName")

HTH MR N
 
Hi,

Thanks for your very fast reply!

Ok, but will the session("item") work on browsers without cookies enabled
on the browser?

And i have a "Public input As New System.Collections.SortedList"
How do i create a sortedlist unique for each session?

Thanks again!

Best regards

Floris
 
So the next question is : How do i declare a sortedlist unique for eachs
ession that i can read from each module file ?
and cannot be read by other sessions ofcourse.

BR

Floris
 
The webconfig can be set to use cookiless operations "Cookie Munging" if not
all your browsers support cookies, this stores the sessionID into the URL

A session is unique to the browser which opened it ( or should be ).
 
Ok i understand the session part.

now the only thing I want to know is how to declare a sortedlist
(System.Collections.SortedList) unique for each session and can be read thru
my entire application (all pages....)

Br

Floris
 
Dim MySortedList as System.Collections.SortedList

In the page load event for your entry page , something like this. . . . .

If Not IsPostBack
MySortedList = DO THE ASSIGNMENT HERE FROM YOUR DATA
Session("MySortedList") = MySortedList
Else
MySortedList = Session("MySortedList")
End If

And for each other non entry page.

MySortedList = Session("MySortedList")
 
And there is no other way that i can like declare a variable in
default.aspx.vb:

dim test as string

and then modify it in functions.vb like:

test = 3

I want all my default functions in a seperate file.... now i stored them in
a module... functions.vb

BR

Floris
 
Hi Floris,
now the only thing I want to know is how to declare a sortedlist
(System.Collections.SortedList) unique for each session and can be read
thru my entire application (all pages....)

That is a bit more complicated. If I understand you correctly, you want a
Sorted List that is unique to every Session, yet available to every other
Session. For that, you need the Application space, in particular, the
Application Cache, which is also a Collection, but is available to every
Session, in fact, to the entire Application.

What I would do is not store the Sorted Lists in Session State, but in the
Application Cache. The Application Cache is a Dictionary like the Session,
so you could set the Key to the Session ID for a particular user Session,
and the Value to the Sorted List that belongs to that Session. Example:

Cache.Insert(Session.SessionID, YourSessionSortedList, null,
DateTime.MaxValue, TimeSpan.FromMinutes(21));

Items in the Application Cache have configurable expiration associated with
them, so another alternative is to set the expiration for the item in the
Application Cache to a little longer than the expiration for the Session.
You would need to add an Event Handler to (probably) the
PreRequestHandlerExecute Event in the global.asax file. The Session should
be available at that time. You can't literally update the expiration of an
item in the Cache, but you can renew it by first obtaining a reference to it
(object = (objectType)Cache["KeyName"] - in this case the SessionID), and
then reinsert it with

Cache.Insert(Session.SessionID, YourSessionSortedList, null,
DateTime.MaxValue, TimeSpan.FromMinutes(21));

As it has the same Key as the original Cache Item, it will overwrite it.

This method will enable the Application Cache to manage itself. One minute
after the Session dies from lack of Requests, the item in the Cache will
also expire.

Finally, avoid using Modules in your app. Not only are they totally global
and singletons, rather than instances, they are not Thread-safe. If you need
a static item, create it in a class. Only that item will be static. The rest
of the class will not be. And be careful in general of using static items.
When you can, use the containers such as Application, Session, and Page to
do your caching.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
I'd rather be a hammer than a nail.
 
Back
Top