Dealing With DBNull values in a database June 21 2004
Just a quick note on how to deal with DBNull values when they are yanked from the DB.
Here is the Example :
Cast from type ‘DBNull’ to type ‘Integer’ 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: Cast from type ‘DBNull’ to type ‘Integer’ is not valid.
Basically the value in the DB was null and I was trying to get an integer.
This is what you do:
dim intMyVal as integer
if(dr.item(”some_field”) is system.DBNull.Value) then
intMyVal = dr.item(”some_field”)
endif
this should get rid of the stupid exception crap!
- Posted in : .NET
- Author : site admin

Comments»
no comments yet - be the first?