Casting Problem

G

Guest

I have an ASP.NET/C# solution, where I can perfectly cast something I stored
in the session object to a class of mine (BackEnd), as this:
->be = (BackEnd)Session["BackEnd"];<-

But if I try to do the same:
->this.backend = (BackEnd)System.Web.HttpContext.Current.Session["BackEnd"]<--
in another project of the same solution I got:
public CustomerAccess(BackEnd backend, string culture) {
this.backend = (BackEnd)System.Web.HttpContext.Current.Session["BackEnd"];
....
at runtime. I can access the Session-state perfectly (because of a hint of
Marcelo in this Newsgroup, so I referenced System.Web and everything looked
okay) and cast for example to (string) without any problem.
What could be the problem here ?
 
G

Guest

and I forgot to paste the error message:

Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.
 
S

Scott Allen

Hi Carlo:

Something must be in the session object you don't expect. Try to run
the application in the debugger and put a breakpoint on the line
causing the problem. Investigate what really exists in
Session["BackEnd"].

--
Scott
http://www.OdeToCode.com


and I forgot to paste the error message:

Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.


Carlo Marchesoni said:
I have an ASP.NET/C# solution, where I can perfectly cast something I stored
in the session object to a class of mine (BackEnd), as this:
->be = (BackEnd)Session["BackEnd"];<-

But if I try to do the same:
->this.backend = (BackEnd)System.Web.HttpContext.Current.Session["BackEnd"]<--
in another project of the same solution I got:
public CustomerAccess(BackEnd backend, string culture) {
this.backend = (BackEnd)System.Web.HttpContext.Current.Session["BackEnd"];
...
at runtime. I can access the Session-state perfectly (because of a hint of
Marcelo in this Newsgroup, so I referenced System.Web and everything looked
okay) and cast for example to (string) without any problem.
What could be the problem here ?
 
G

Guest

Thank you for your hint Scott. I tried it and it drives me even more crazy,
since in the debugger it works, but the same line "at runtime" fails.
I put a breakpoint before the line, and inspected the Session variable:
?System.Web.HttpContext.Current.Session["BackEnd"]
{BackEnd}
System.Object: {BackEnd}
con: {com.Transoft.TCA.TCAConnection}

looks okay, so I went ahead and assigned it to my private field, casting the
session variable:
this.backend = (BackEnd)System.Web.HttpContext.Current.Session["BackEnd"]
{BackEnd}
System.Object: {BackEnd}
con: {com.Transoft.TCA.TCAConnection}

and it works as well !!!
But now comes the thing I don't understand: if I step to the next statement,
which is exactly what I entered in the debugger window, it fails with the
Specified cast is not valid
message.

Scott Allen said:
Hi Carlo:

Something must be in the session object you don't expect. Try to run
the application in the debugger and put a breakpoint on the line
causing the problem. Investigate what really exists in
Session["BackEnd"].

--
Scott
http://www.OdeToCode.com


and I forgot to paste the error message:

Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.


Carlo Marchesoni said:
I have an ASP.NET/C# solution, where I can perfectly cast something I stored
in the session object to a class of mine (BackEnd), as this:
->be = (BackEnd)Session["BackEnd"];<-

But if I try to do the same:
->this.backend = (BackEnd)System.Web.HttpContext.Current.Session["BackEnd"]<--
in another project of the same solution I got:
public CustomerAccess(BackEnd backend, string culture) {
this.backend = (BackEnd)System.Web.HttpContext.Current.Session["BackEnd"];
...
at runtime. I can access the Session-state perfectly (because of a hint of
Marcelo in this Newsgroup, so I referenced System.Web and everything looked
okay) and cast for example to (string) without any problem.
What could be the problem here ?
 
S

Scott Allen

Hi Carlo:

This does seem quite strange. I'm out of ideas on why it might behave
this way, unless there is some conversion operator or COM interop
going on behind the scences.

--s

Thank you for your hint Scott. I tried it and it drives me even more crazy,
since in the debugger it works, but the same line "at runtime" fails.
I put a breakpoint before the line, and inspected the Session variable:
?System.Web.HttpContext.Current.Session["BackEnd"]
{BackEnd}
System.Object: {BackEnd}
con: {com.Transoft.TCA.TCAConnection}

looks okay, so I went ahead and assigned it to my private field, casting the
session variable:
this.backend = (BackEnd)System.Web.HttpContext.Current.Session["BackEnd"]
{BackEnd}
System.Object: {BackEnd}
con: {com.Transoft.TCA.TCAConnection}

and it works as well !!!
But now comes the thing I don't understand: if I step to the next statement,
which is exactly what I entered in the debugger window, it fails with the
Specified cast is not valid
message.
 
G

Guest

Thanks to your comments I understood that it MUST work and in fact now it
does. The problem was:

As I told you I have a solution with several projects. The class (BackEnd)
which gave the casting error when casting to had no NAMESPACE and was present
in the ASP.NET project AND in my BLL project (same code).
I assigned the Session["BackEnd"] in the ASP.NET project and somehow the
Framework must know that, even if it has the same name, it is a class of
another project. The strange thing is still that it works when entering the
command in the debugger, but it fails at runtime.
I put the class in a namespace in a new project and reference this new
project from my ASP.NET project and from the BLL project and now it works.
Thanks again for your help.

Scott Allen said:
Hi Carlo:

This does seem quite strange. I'm out of ideas on why it might behave
this way, unless there is some conversion operator or COM interop
going on behind the scences.

--s

Thank you for your hint Scott. I tried it and it drives me even more crazy,
since in the debugger it works, but the same line "at runtime" fails.
I put a breakpoint before the line, and inspected the Session variable:
?System.Web.HttpContext.Current.Session["BackEnd"]
{BackEnd}
System.Object: {BackEnd}
con: {com.Transoft.TCA.TCAConnection}

looks okay, so I went ahead and assigned it to my private field, casting the
session variable:
this.backend = (BackEnd)System.Web.HttpContext.Current.Session["BackEnd"]
{BackEnd}
System.Object: {BackEnd}
con: {com.Transoft.TCA.TCAConnection}

and it works as well !!!
But now comes the thing I don't understand: if I step to the next statement,
which is exactly what I entered in the debugger window, it fails with the
Specified cast is not valid
message.
 
S

Scott Allen

Oooh, that is tricky. Good catch, glad it works!

--s

Thanks to your comments I understood that it MUST work and in fact now it
does. The problem was:

As I told you I have a solution with several projects. The class (BackEnd)
which gave the casting error when casting to had no NAMESPACE and was present
in the ASP.NET project AND in my BLL project (same code).
I assigned the Session["BackEnd"] in the ASP.NET project and somehow the
Framework must know that, even if it has the same name, it is a class of
another project. The strange thing is still that it works when entering the
command in the debugger, but it fails at runtime.
I put the class in a namespace in a new project and reference this new
project from my ASP.NET project and from the BLL project and now it works.
Thanks again for your help.

Scott Allen said:
Hi Carlo:

This does seem quite strange. I'm out of ideas on why it might behave
this way, unless there is some conversion operator or COM interop
going on behind the scences.

--s

Thank you for your hint Scott. I tried it and it drives me even more crazy,
since in the debugger it works, but the same line "at runtime" fails.
I put a breakpoint before the line, and inspected the Session variable:
?System.Web.HttpContext.Current.Session["BackEnd"]
{BackEnd}
System.Object: {BackEnd}
con: {com.Transoft.TCA.TCAConnection}

looks okay, so I went ahead and assigned it to my private field, casting the
session variable:
this.backend = (BackEnd)System.Web.HttpContext.Current.Session["BackEnd"]
{BackEnd}
System.Object: {BackEnd}
con: {com.Transoft.TCA.TCAConnection}

and it works as well !!!
But now comes the thing I don't understand: if I step to the next statement,
which is exactly what I entered in the debugger window, it fails with the
Specified cast is not valid
message.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top