InvalidCastException exception when you reference the value of a DataColumn that is NULL (310371)
The information in this article applies to:
- Microsoft ADO.Net 2.0
- Microsoft ADO.NET (included with the .NET Framework)
- Microsoft Visual Basic 2005
- Microsoft Visual Basic .NET (2002)
- Microsoft Visual Basic .NET (2003)
This article was previously published under Q310371
This article refers to the following Microsoft .NET Framework Class Library namespaces:
- System.Data
- System.Data.SqlClient
SYMPTOMS
When you reference the value of a DataColumn that is NULL, whether typed or untyped, you receive an exception.
For typed DataSets, you receive the following exception:
An unhandled exception of type 'System.Data.StrongTypingException' occurred in invalidcast.exe
Additional information: Cannot get value because it is DBNULL.
For untyped DataSets, you receive the following exception:
An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
Additional information: Cast from type 'DBNULL' to type 'String' is not valid.
CAUSECause 1
When you use a typed DataSet, the exception is caused when you reference the column value that is NULL.
Cause 2
When you use an untyped DataSet, the value of the column is NULL, and the variable to which you assign it does not support NULL.
RESOLUTIONCause 1
Test the column for NULL before you access its value. In an untyped DataSet, use the DataRow.IsNull method. In a typed DataSet, use the DataRow.IscolumnnameNull property.
Cause 2
Use a variable of an appropriate data type that can store NULL.
STATUS
This behavior is by design.
REFERENCES
For more information about creating a typed DataSet, click the following article number to view the article in the Microsoft Knowledge Base:
315678
How to create, use typed DataSet by using Visual Basic .NET
For more information about ADO.NET, click the following article number to view the article in the Microsoft Knowledge Base:
Modification Type: | Minor | Last Reviewed: | 10/3/2006 |
---|
Keywords: | kbprb kbSystemData KB310371 kbAudDeveloper |
---|
|