ASP.NET multiple users/ single class files

G

Guest

I have made an ASP.NET web application that connects to SQL Server, reading
and writing data using classes. I was recommended to use session objects to
store the data per user, because each user using the application needs to see
their own data only. My problem is that when multiple users are in the
application, when in the session object, data is fine, however as there is
only one instance of the class files, when data is put into them, every
user's data is mixed. Can classes be used with ASP.NET for this purpose? If
so, how?!? I'm confused because in Windows applications each user has their
own executables and class files locally, however in a web application, each
user uses the same class files - are they replicated in memory or is there
only the one set of class files? If the latter then surely they will always
be overwritten.
Any help would be great, thanks

For my original problem and the resulting solution, please search on this
string:
"Problem with asp.net app only allowing 1 user"
 
H

Hans Kesting

jsale said:
I have made an ASP.NET web application that connects to SQL Server,
reading and writing data using classes. I was recommended to use
session objects to store the data per user, because each user using
the application needs to see their own data only. My problem is that
when multiple users are in the application, when in the session
object, data is fine, however as there is only one instance of the
class files, when data is put into them, every user's data is mixed.
Can classes be used with ASP.NET for this purpose? If so, how?!? I'm
confused because in Windows applications each user has their own
executables and class files locally, however in a web application,
each user uses the same class files - are they replicated in memory
or is there only the one set of class files? If the latter then
surely they will always be overwritten.
Any help would be great, thanks

For my original problem and the resulting solution, please search on
this string:
"Problem with asp.net app only allowing 1 user"

If you use a different *instance* for each user, there should not be a problem.
Only use a singleton if you really mean there should be only one instance
in the entire application.

If you are writing information to a temporary file (as you seem to do from
your other post), then you need to make sure every user (read: session)
uses a different file. I'm not sure this is the best route though! (performance
issues, security issues, diskspace, ...)

I think a better way is:
- define (in your code) a class to hold all data you want to collect
- for each session, create an instance of this class and store it in Session
- if you want to store some data in that object, retrieve a reference to
it from the Session and update you object.
- when you are ready to store everything (for this user) in the database,
again retrieve a reference, read everything and store it in the database.
Then you can remove this object from the user's Session.

The fact that the *code* is shared in the web-application doesn't mean that
the *values* are shared too. Unless you specifically want to, that is.

Show some code how you persist that temporary data now, and we can point
out specific issues with it.

Hans Kesting
 
G

Guest

I'm not using singletons, what I meant was that when I created the web
application, the folder houses a number of web pages (aspx pages) as well as
class files. When a user uses my application and selects a site from the
database, a functions module retrieves the data and creates a new instance of
the class (housed in a class collection) where required. I can't really put
any meaningful code on here for you because the many different classes are
100s lines of code each!
By 'single instance', i meant that the classes only exist on the web server,
not on seperate computers like windows applications where each user has their
local copy.
 
G

Guest

If i can explain a little better, i don't use singletons, it's just that
whereas a windows application has a local copy (i.e. one per user) of all the
classes, only the one user is writing to them. With the web application, the
confusion i am getting is that the web server only has one physical copy of
the classes, but is accessed many times by many users, therefore, how does it
'know' which user is using the classes at any one time?
The sort of structure i am using is that when data is retrieved from the DB,
it is stored in class files, which in turn are saved into session objects (so
each user has their and only their data available):
Session("Project") = Project 'where project is the main class
The problem is that when the data is taken out of the session and put back
into the classes for amendment, any subsequent users using the classes at the
time are looking at the 1st users' data.
Sorry if i'm not making myself clear, i'm trying to get my head around this
as well. Thanks for replying!
 
H

Hans Kesting

jsale said:
I'm not using singletons, what I meant was that when I created the web
application, the folder houses a number of web pages (aspx pages) as
well as class files. When a user uses my application and selects a
site from the database, a functions module retrieves the data and
creates a new instance of the class (housed in a class collection)
where required. I can't really put any meaningful code on here for
you because the many different classes are 100s lines of code each!
By 'single instance', i meant that the classes only exist on the web
server, not on seperate computers like windows applications where
each user has their local copy.

You need to make a distinction between "code that defines a class"
and "instance of a class". With that single copy of the class-code you can create
any number of independent instances:

-------------------
class MyClass
{
public int val;
}

MyClass c1 = new MyClass();
MyClass c2 = new MyClass();

c1.val = 1;
c2.val = 2;

Console.WriteLine(c1.ToString()); <---- gives 1
Console.WriteLine(c2.ToString()); <---- gives 2
 
E

Eliyahu Goldin

Looks like you are misunderstanding the whole idea how web servers work. It
is not like there is one instance of your program running all the time and
serving all requests. Web servers serve http requests. When a request comes,
server starts your program, builds a response, sends it back to client and
disposes your program. Your objects get killed at that stage. When another
request comes, your program will be called another time and new objects will
be created.

Eliyahu
 
M

Matt Berther

Hello jsale,

If you are using static variables on the classes in questions, this could
be a cause of your problem.
 
H

Hans Kesting

Hans said:
You need to make a distinction between "code that defines a class"
and "instance of a class". With that single copy of the class-code
you can create any number of independent instances:

-------------------
class MyClass
{
public int val;
}

MyClass c1 = new MyClass();
MyClass c2 = new MyClass();

c1.val = 1;
c2.val = 2;

Console.WriteLine(c1.ToString()); <---- gives 1

eh, this should have been
Console.WriteLine(c1.val.ToString());
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top