Connecting Tech Pros Worldwide Forums | Help | Site Map

Casting Problem

Carlo Marchesoni
Guest
 
Posts: n/a
#1: Nov 18 '05
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 ?


Carlo Marchesoni
Guest
 
Posts: n/a
#2: Nov 18 '05

re: Casting Problem


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" wrote:
[color=blue]
> 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 ?
>[/color]
Scott Allen
Guest
 
Posts: n/a
#3: Nov 18 '05

re: Casting Problem


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


On Tue, 17 Aug 2004 13:21:02 -0700, "Carlo Marchesoni"
<CarloMarchesoni@discussions.microsoft.com> wrote:
[color=blue]
>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" wrote:
>[color=green]
>> 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 ?
>>[/color][/color]

Carlo Marchesoni
Guest
 
Posts: n/a
#4: Nov 18 '05

re: Casting Problem


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" wrote:
[color=blue]
> 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
>
>
> On Tue, 17 Aug 2004 13:21:02 -0700, "Carlo Marchesoni"
> <CarloMarchesoni@discussions.microsoft.com> wrote:
>[color=green]
> >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" wrote:
> >[color=darkred]
> >> 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 ?
> >>[/color][/color]
>
>[/color]
Scott Allen
Guest
 
Posts: n/a
#5: Nov 18 '05

re: Casting Problem


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

On Tue, 17 Aug 2004 23:11:02 -0700, "Carlo Marchesoni"
<CarloMarchesoni@discussions.microsoft.com> wrote:
[color=blue]
>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.
>[/color]

--
Scott
http://www.OdeToCode.com
Carlo Marchesoni
Guest
 
Posts: n/a
#6: Nov 18 '05

re: Casting Problem


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" wrote:
[color=blue]
> 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
>
> On Tue, 17 Aug 2004 23:11:02 -0700, "Carlo Marchesoni"
> <CarloMarchesoni@discussions.microsoft.com> wrote:
>[color=green]
> >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.
> >[/color]
>
> --
> Scott
> http://www.OdeToCode.com
>[/color]
Scott Allen
Guest
 
Posts: n/a
#7: Nov 18 '05

re: Casting Problem



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

--s

On Wed, 18 Aug 2004 12:31:02 -0700, "Carlo Marchesoni"
<CarloMarchesoni@discussions.microsoft.com> wrote:
[color=blue]
>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" wrote:
>[color=green]
>> 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
>>
>> On Tue, 17 Aug 2004 23:11:02 -0700, "Carlo Marchesoni"
>> <CarloMarchesoni@discussions.microsoft.com> wrote:
>>[color=darkred]
>> >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.
>> >[/color]
>>
>> --
>> Scott
>> http://www.OdeToCode.com
>>[/color][/color]

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