ToDotNet

Convert.ToDotNet(InstanceOfWorld)

  Home :: Contact :: Syndication  :: Login
  172 Posts :: 0 Stories :: 175 Comments :: 22 Trackbacks

News





My Vista Score...
3.4




Archives

Post Categories

Image Galleries

Affiliations

Blogs I Read

Interesting Links

My Links

Strange things occur sometimes. Like this error message I get when running my project under development:

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: Cast from type 'DBNull' to type 'Integer' is not valid.

Source Error:


Line 1348:            Get
Line 1349:                Try 
Line 1350:                    Return CType(Me(Me.tableCP_P_WEBMODULES.WEBMODULE_IDColumn),Integer)
Line 1351:                Catch e As InvalidCastException
Line 1352:                    Throw New StrongTypingException("Cannot get value because it is DBNull.", e)

So the faulty code is in enclosed in a Try-Catch statement. The Catch-block catches an InvalidCastException. However, when that actual type of exception is thrown, it's not handled.

Surely, I can make sure that this code never throws an exception by checking the datatype before calling the method. But that doesn't explain why the exception is not caught right here. Ideas anyone?

posted on Thursday, February 19, 2004 4:56 PM



Feedback

# re: When is an InvalidCastException not an InvalidCastException? 2/19/2004 5:58 PM Paul Gielens
StrongTypingException is being thrown and isn't being handled up the stack.

Why not check:

if (tableCP_P_WEBMODULES.WEBMODULE_IDColumn != DBNull.Value)
{
int.Parse(tableCP_P_WEBMODULES.WEBMODULE_IDColumn);
}

# re: When is an InvalidCastException not an InvalidCastException? 2/19/2004 8:20 PM Sander
If a StrongTypingException was thrown, it would not say "System.InvalidCastException" being the unhandled exception, right?
Besides, this is automatically generated code for a typed dataset.

# re: When is an InvalidCastException not an InvalidCastException? 1/18/2005 6:53 PM Jo
I get exactly the same thing.
This is code from the generated Dataset in Visual Studio.
Only way around I found, is to test upfront with the typed row method is<Column>Null and not using the attribute if it is.
If there is another solution i would love to hear it.


Jo.

# re: When is an InvalidCastException not an InvalidCastException? 11/10/2005 10:44 AM Adrian
Jo could you elaborate on your way round it please? I'm sick of running into this and I no longer think altering the code in the typed dataset is a good idea especially since any changes mean the code you alter is reverted back to default.

Post Feedback

Title:
Name:
Url:
Comments: 
Protected by Clearscreen.SharpHIPEnter the code you see: