473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Casting Problem

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 ?

Nov 18 '05 #1
6 1370
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:
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 ?

Nov 18 '05 #2
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"
<Ca*************@discussions.microsoft.com> wrote:
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:
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 ?


Nov 18 '05 #3
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:
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"
<Ca*************@discussions.microsoft.com> wrote:
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:
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 ?


Nov 18 '05 #4
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"
<Ca*************@discussions.microsoft.com> wrote:
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
http://www.OdeToCode.com
Nov 18 '05 #5
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:
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"
<Ca*************@discussions.microsoft.com> wrote:
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
http://www.OdeToCode.com

Nov 18 '05 #6

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

--s

On Wed, 18 Aug 2004 12:31:02 -0700, "Carlo Marchesoni"
<Ca*************@discussions.microsoft.com> wrote:
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:
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"
<Ca*************@discussions.microsoft.com> wrote:
>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
http://www.OdeToCode.com


--
Scott
http://www.OdeToCode.com
Nov 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: destroyedlolo | last post by:
Hi all, I just upgrade my workstation to Gcc 3.3, and I got a very strange probleme with casting : I got an error if I try to get the adress of a "casted" pointer. ---- dumy example source...
2
by: ghostdog | last post by:
hi, i got this opengl/c++ code: <code> void render(CMesh *mesh){ ... float *pVertices; int *pIndices;
4
by: Harry | last post by:
Hi ppl Some problem regarding pointer: ULONG *addr; CHAR *ptr=NULL; addr=new ULONG; //calling a function and getting the addr value //ULONG is 4 bytes. I am getting the Ip address which...
2
by: philippe sillon | last post by:
Hi, I have a problem to implemente the strategy pattern. the problem come from that a method take different arguments type (object in interface and String / Int in implemented class). When...
1
by: fabrice | last post by:
Hello I have a little problem with casting in a Datagrid Control. I'm using Option Strict in my web application. In a Template Column, I put in an ImageButton Control with a Command...
0
by: Greg | last post by:
Not sure if this is best place for this problem, but here it is. I have a project that is simply a C# class that interfaces with an IFilter. This is so I can retreive the text from Word docs. ...
2
by: stunt016 | last post by:
I have a program written in C# that handles communication between two pieces of software. My problem only deals with getting a text array from one program to this C# "Bridge". I can get the text...
7
by: Ajeet | last post by:
hi I am having some difficulty in casting using generics. These are the classes. public interface IProvider<PROF> where PROF : IProviderProfile { //Some properties/methods }
1
by: guncrew | last post by:
Hello I am starting to use Python. I found a nice little example for subsituting and splitting texts and counting words separated by defined expressions (in the example , and blanks). Unfotunately...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.