A first chance exception of type 'System.InvalidCastException' occurred in ***.exe  
Author Message
AlexBB





PostPosted: Visual C# General, A first chance exception of type 'System.InvalidCastException' occurred in ***.exe Top

A first chance exception of type 'System.InvalidCastException' occurred in tapTCPports_1.exe

I get this runtime error and a message that a "specified cast is not valid." I sort of know that. The problem is where to look

The Output Window gives me that general pointer as above. Immediate Window is empty. What MSDN help says on the subject of Command Window at ms-help://MS.VSCC.v80/MS.MSDN.vAug06.en/dv_vscmds/html/48711628-1909-4713-a73e-d7b714c77f8a.htm is not encouraging and useless for my problem as I can see.

Since early in the morning I have written perhaps 3 dozen cast statements. Where shall I go

How do you people handle this problem I am sure there is a quick and simple way to locate the errant line.

Thanks.




Visual C#14  
 
 
AlexBB





PostPosted: Visual C# General, A first chance exception of type 'System.InvalidCastException' occurred in ***.exe Top

Just after I posted this I tried Debug.ListCallStack command. Reply: Call stack is not available at this time. So I have to wait:)

 
 
Brendan Grant





PostPosted: Visual C# General, A first chance exception of type 'System.InvalidCastException' occurred in ***.exe Top

Any chance you could share a bit of the code where this Exception is occurring

Edit: Also... if you are getting this exception from the CLR... what kind of info is the details window providing Is this a debug or release build



 
 
AlexBB





PostPosted: Visual C# General, A first chance exception of type 'System.InvalidCastException' occurred in ***.exe Top

Any chance you could share a bit of the code where this Exception is occurring

Edit: Also... if you are getting this exception from the CLR... what kind of info is the details window providing Is this a debug or release build

It is a debug. I get call tracks and can see locals with any runtime error but typically not when the types are miscast. I said "typically" although I am not sure, perhaps it is always. I will now pay more attention. Before I would just on a hunch get back to the code but on that day it was too much: I did a massive copying many blocks with some cast changes. I decided to clear it up once and for all. I was lucky and stumbled on the offending statement and it was only one.

There is not much to the code, actually. It is all monotony. Most likely it was the following block. I am compacting rows in a table, copying field values from one row to another and then deleting extra rows:

if ( ( Int32 )dtRow[1].ItemArray.GetValue ( 5 ) > 0 & ( Int32 )dtRow[0].ItemArray.GetValue ( 5 ) == 0 )

{ // volume

dtRow[0].ItemArray.SetValue ( dtRow[1].ItemArray.GetValue ( 5 ), 5 );

}

I cast it as ( Double ) by mistake because I copied the whole block from another field.

I cannot answer your question about the CLR. It is .NET 2.0 for sure. How can I find out

Thanks.