Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session variable with Class

Status
Not open for further replies.

croydon

Programmer
Apr 30, 2002
253
EU
I am changing an application that includes a public class.

I am adding a session variable that will pass between forms and hold any error messages encountered. This works fine except when it accesses the class. Then the session contents are lost.

I added a watch and when it enters the class I get the message 'Name session is not decared'.

I tried adding 'Inherits System.Web.UI.Page' but I get the error 'Cannot refer to an instance member of a class from within a shared method......'.

Any suggestions would be appreciated.

 
Try using:
Code:
httpcontext.Current.Session("mySessionVar") = "Test Val"
Jim
 
Thanks Jim. I don't think I've made myself clear though.

Here is the problem:

1. In the webpage an error message is added to Session("ErrorMsg"), e.g. "Address not entered".

2. The program then use a subroutine within the class. I do not use any Session variables in the class but the watch gives the message 'Name Session is not declared".

3. When processing returns to the page the contents of Session("ErrorMsg") is "".
 
Ok I see, well it seems somewhere the session var is being set to "". Have you stepped though all of the code and see if there is code somewhere that sets it to ""? Also are you sure the session var is being set correctly and has a value before the class sub is called?

Jim
 
Jim, yes I've stepped through the code and I've forced some text into Session("ErrorMsg"). It's when it reaches the class that I get the watch error. When it returns to the page, there is no longer an error message but the variable has lost it's content.
 
Calling a class should not clear the session vars, I do this all the time. This is very strange. Try this just for kicks.... Don't put a stop in your code until you are out of the class, say the next line after the calling line of code, then check the session variable.

BTW, you will get an error message when referring to session variables in a class. A class does not contain a session, that's why I supplied the code before. I thought you wanted to set or access a session variable in code.

Jim

 
Jim, I tracked it down to the process running through "If Not IsPostBack" (where the variable is emptied) twice. I'm not sure why this is as I did not write the original program, but I shall look into it.

Thanks again for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top