471,123 Members | 833 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,123 software developers and data experts.

Session Variable-InvalidCastException

I use a Session variable to store a custom object (all properties contain
strings) between postbacks. On occasion, mostly when I change and save code,
when I refresh the web page to see the changes, I get an error:

"System.InvalidCastException was unhandled by user code -Unable to cast
object of type 'QueryParameters' to type 'QueryParameters'"

in the function below which is called during Page_Load (_queryParameters =
QueryParameters.getInstance())

Public Shared Function getInstance() As QueryParameters
Dim instance As QueryParameters
If IsNothing(Current.Session(SESSION_VARIABLE)) Then
instance = New QueryParameters()
instance.saveQPToSession()
Else
instance = Current.Session(SESSION_VARIABLE)
End If
Return instance
End Function

When I debug, a valid Session variable exists because the code goes to the
Else statement, and in the Watch window, I can see all the properties of the
custom object in the Session variable. Since 'instance' is typed as
QueryParameters and a Session variable exists that holds the object, I don't
understand why there is an error since these both seem to be typed correctly
as QueryParameters. Even when I explicitly type the session variable
(CType(Current.Session(SESSION_VARIABLE), QueryParameters)) the same error
appears.

If I close the browser completely, everything works, but I hate to have to
close a browser with every iteration of code changes.

Does anyone know why this error appears? Thanks for any help.
Apr 19 '07 #1
2 2045
Hi,

Don Miller wrote:
I use a Session variable to store a custom object (all properties contain
strings) between postbacks. On occasion, mostly when I change and save code,
when I refresh the web page to see the changes, I get an error:

"System.InvalidCastException was unhandled by user code -Unable to cast
object of type 'QueryParameters' to type 'QueryParameters'"
The symptoms make me think of that error:
http://weblogs.asp.net/scottgu/archi...on-issues.aspx

Could that be it?

Greetings,
Laurent
>
in the function below which is called during Page_Load (_queryParameters =
QueryParameters.getInstance())

Public Shared Function getInstance() As QueryParameters
Dim instance As QueryParameters
If IsNothing(Current.Session(SESSION_VARIABLE)) Then
instance = New QueryParameters()
instance.saveQPToSession()
Else
instance = Current.Session(SESSION_VARIABLE)
End If
Return instance
End Function

When I debug, a valid Session variable exists because the code goes to the
Else statement, and in the Watch window, I can see all the properties of the
custom object in the Session variable. Since 'instance' is typed as
QueryParameters and a Session variable exists that holds the object, I don't
understand why there is an error since these both seem to be typed correctly
as QueryParameters. Even when I explicitly type the session variable
(CType(Current.Session(SESSION_VARIABLE), QueryParameters)) the same error
appears.

If I close the browser completely, everything works, but I hate to have to
close a browser with every iteration of code changes.

Does anyone know why this error appears? Thanks for any help.

--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Apr 20 '07 #2
>"System.InvalidCastException was unhandled by user code -Unable to cast
>object of type 'QueryParameters' to type 'QueryParameters'"

The symptoms make me think of that error:
http://weblogs.asp.net/scottgu/archi...on-issues.aspx

Could that be it?
I tried the simple workaround (changing the web config file system "batch"
to false) and the error message persisted (run the application, type in
garbage in the code-behind, erase the garbage, save the file, hit refresh in
the browser). I am leary of applying the hotfix. I may just end up putting
my properties into hidden fields instead of an object in a Session variable
and forgetting this bug.

Something to do with changing the source code file and compilation.

Apr 20 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by William LaMartin | last post: by
9 posts views Thread by Greg Linwood | last post: by
4 posts views Thread by T Ralya | last post: by
3 posts views Thread by Alan Wang | last post: by
17 posts views Thread by Control Freq | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.