|
-
Jan 14th, 2006, 01:19 AM
#1
Thread Starter
New Member
Session Variable - Invalid cast exeption
I got a class (SessionVariables.cs) of which i want to use one instance for one user's session,
So, when the app start (in session_start), I create a new instance of the class and add it to the session:
Code:
SessionVariables sessionvars = new SessionVariables();
this.Session.Add("sessionvars", sessionvars);
On each form or control that should use this class, I declare an instance of the class at the top of my code:
Code:
private SessionVariables sessionvars;
and in the form load event, set this instance to the instance of my class:
Code:
sessionvars = (accuracyauto.SessionVariables)Session["sessionvars"];
However, SOMETIMES, I get an invalid cast exception when reaching the last line. Most times it go through that line without any problems. This only happen on my main page (default.aspx), however I do not use this class any different on this page than any other control or page.
I never destroy this instance of the class anywhere in my application.
any ideas?
currently I work around this by saving each class property in seperate session variables, and use a try, catch to set the class instance in this form. If an error occur, i create a new instance (in the catch block), set it's properties to what I saved as backup, and add it to the session again.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|