classes under asp.net

B

Bruno Alexandre

hi guys,

because asp.net class has a livetime of a active webpage (soon the user
changed the page, all the values saved in the class is lost)...

how do you manage to pass this?


my idea was in global.asax create an application session like

dim myClassVar as myClass
Application.unlock()
Applicaion("myClass") = myClassVar
Application.lock()


and then in all the pages do

dim myClassVar as new myClass

inside Page_load
myClassVar = Application("myClass")


....

any other ideias..., cause like this I can't call the apllication variable
inside a new class!
 
M

mnichols

Hello,

Yes, if you need to access the same object for all users then store it
in Application("myClass").

If you need to access the same object for a single user then store it in
Session("myClass").

mnichols
 
B

Bruno Alexandre

but I have another class, that get's a value from this one.
How can I get the class inside a 2nd class?


I have Portal Class that has all values/functions and subs to manipulate a
user
I have Stocks Class that has the same but only for Stocks proposals
I have Warranty Class that has the same for Product warranty information

inside Stocks Class I call the Portal.idDistributor (the Distributor ID) so
I can retrieve only his stock, but I get an error everytime I do the
Dim myPortal as new Portal
myPortal = session("myPortalClass")
inside the STocks Class, it underlines the session saying that is not
defined...

then I change it to:
Dim mySession As HttpApplicationState = Nothing
Dim myPortal As Portal = mySession("myPortalClass")

but I do not have the same objects in that!

how can I acess it?
 
M

mnichols

I am not a VB person, but I think the basic problem is that your class
does not know about the page or web-site. So try these:

HttpContext.Application("")
HttpContext.Session("")

They should return the current Application and Session objects.

mnichols
 
B

Bruno Alexandre

still, you right.

just a little add, the correct code is:

HttpContext.Current.Session("myPortalClass")


thank you.
 
J

Jason Coyne

The other posters have correctly shown you how to store items in the
session or application stores. However, I must caution you that placing
a lot of objects into the session can hurt performance and scalability
quite a bit. Make sure you do not keep open expensive resources (such
as database connections) in your session variables.

Unless your object has an expensive buildup/teardown process, it is
probably better to re-instantiate the object during every page view.

I cannot stree snough the importance that you not store a database or
other "heavy" connection in your session. Make sure you follow the
principle of "aquire late, release early" in web design.

In addition to the scalability issues that session variables bring, you
also have to dael with concurernce. If the same user has multiple
browsers open, you may have one sessino shared between both browsers,
but each of the browsers wants a different instance of your object
(because they are looking at different information for example)
 
B

Bruno Alexandre

I only have values and functions, the functions open and close database
connections, and the values are for user profile, such as username, accesses
(boolean), birthdate, name, office name, etc...

so what you are saying is that a Session is diferent for the
browser/client/user but only if they not in the same network?
imagine there are 3 computers in a company and all of them are using the
same version of the same browser and they are all in the web application...

how can I store such information to have it diferent for all of them?

I have the same application running in classic ASP (asp 3.0) and I never had
problems like that, of course in asp 3.0 I never used classes, only here in
asp.net 2.0.

--

Bruno Alexandre
(a Portuguese in Københanv, Danmark)


"Jason Coyne" <[email protected]> escreveu na mensagem
The other posters have correctly shown you how to store items in the
session or application stores. However, I must caution you that placing
a lot of objects into the session can hurt performance and scalability
quite a bit. Make sure you do not keep open expensive resources (such
as database connections) in your session variables.

Unless your object has an expensive buildup/teardown process, it is
probably better to re-instantiate the object during every page view.

I cannot stree snough the importance that you not store a database or
other "heavy" connection in your session. Make sure you follow the
principle of "aquire late, release early" in web design.

In addition to the scalability issues that session variables bring, you
also have to dael with concurernce. If the same user has multiple
browsers open, you may have one sessino shared between both browsers,
but each of the browsers wants a different instance of your object
(because they are looking at different information for example)
 
B

Bruno Alexandre

I was look depper into my application and for the entire application I
really only need 4 variables

idUser + idDIstributor + nameUser + nameDistributor

and all the access that that user have, but I can have it in one session
variable like a string and separate the values with a coma, than I can
retrive then using the split function...

I guess I will use this tactic instead saving the hole class inside the
sessions... for future sake of the application performance.

thank you 4 the thought.

--

Bruno Alexandre
(a Portuguese in Københanv, Danmark)


"Jason Coyne" <[email protected]> escreveu na mensagem
The other posters have correctly shown you how to store items in the
session or application stores. However, I must caution you that placing
a lot of objects into the session can hurt performance and scalability
quite a bit. Make sure you do not keep open expensive resources (such
as database connections) in your session variables.

Unless your object has an expensive buildup/teardown process, it is
probably better to re-instantiate the object during every page view.

I cannot stree snough the importance that you not store a database or
other "heavy" connection in your session. Make sure you follow the
principle of "aquire late, release early" in web design.

In addition to the scalability issues that session variables bring, you
also have to dael with concurernce. If the same user has multiple
browsers open, you may have one sessino shared between both browsers,
but each of the browsers wants a different instance of your object
(because they are looking at different information for example)
 
H

Hans Kesting

I only have values and functions, the functions open and close database
connections, and the values are for user profile, such as username, accesses
(boolean), birthdate, name, office name, etc...

so what you are saying is that a Session is diferent for the
browser/client/user but only if they not in the same network?
imagine there are 3 computers in a company and all of them are using the same
version of the same browser and they are all in the web application...

how can I store such information to have it diferent for all of them?

I have the same application running in classic ASP (asp 3.0) and I never had
problems like that, of course in asp 3.0 I never used classes, only here in
asp.net 2.0.

Sessions work about the same in ASP3 and ASP.net. They depend on a
cookie that is remembered by a browser instance.
Users behind different computers use different browser instances.
If a single user uses ctrl-N (or File | New | Window), he will open a
second window for the same browser instance. These windows share the
same session.

Hans Kesting
 

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,787
Messages
2,569,631
Members
45,338
Latest member
41Pearline46

Latest Threads

Top