Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
InvalidCastException exception when you reference the value of a DataColumn that is NULL
This article was previously published under Q310371
This article refers to the following Microsoft .NET Framework Class Library namespaces:
- System.Data
- System.Data.SqlClient
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.
Cause 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.
Cause 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.
This behavior is by design.
Steps to reproduce the behavior
- Start Microsoft Visual Studio .NET.
- Create a new Windows Application project in Visual Basic .NET. Form1 is added to the project by default.
- Make sure that your project contains a reference to the System.Data namespace.
- Place a Button control on Form1.
- Change the Name property of the button to btnUntype and the Text property to Untype.
- Use the Imports statement in the System namespace and the System.Data namespace, so that you are not required to qualify declarations in those namespaces later in your code. Add the following code to the "General Declarations" section of Form1:
Imports System
Imports System.Data
Imports System.Data.SqlClient
- Add the following code in the btnUntype event:
Dim str As String
Dim ds As New DataSet()
Dim r As DataRow
Dim con As New SqlConnection("server=myserver;integrated security=sspi;database=northwind")
Dim da As New SqlDataAdapter("select * from customers where customerid='ANTON'", con)
da.Fill(ds, "customer")
r = ds.Tables(0).Rows(0)
'Uncomment to check for Null.
'If r.IsNull("fax") Then
' MessageBox.Show("Value is Null")
'End If
str = r("fax") 'Comment line to check for Null.
MessageBox.Show(str) 'Comment line to check for Null.
- Modify the connection string (myserver) as appropriate server name for your
environment.
- Save your project. On the Debug menu, click Start to run your project.
- Click Untype. Notice the earlier-mentioned exception for an untyped DataSet.
- Uncomment the IF statement, and then comment the lines below the IF statement to check for NULL.
- Save your project. On the Debug menu, click Start, and then run your project. Notice that the message box displays "Value is Null".
- Place another Button control on Form1.
- Change the Name property of the button to btntype and the Text property to type.
- Create a typed DataSet by using the customers table in the Northwind database. Rename the SQLDataAdapter to sqlda.
- Add the following code to the btntype event:
sqlda.Fill(DataSet11)
Dim str As String
Dim int As Integer
Dim cust As DataSet1.CustomersRow
cust = DataSet11.Customers.Rows(0)
'Uncomment to catch null values.
'If cust.IsFaxNull Then
' MessageBox.Show("Value is Null")
'End If
str = cust.Fax()
MessageBox.Show(str)
' Comment line to check for Null.
- Run the application, and then click the type button. Notice the exception.
- Uncomment the If...Then block of code, and then comment the last two lines of code.
- Save the application, and then run the application and notice the message box.
For more information about creating a typed DataSet, click the following article number to view the article in the Microsoft Knowledge Base:
315678
(http://kbalertz.com/Feedback.aspx?kbNumber=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:
313590
(http://kbalertz.com/Feedback.aspx?kbNumber=313590/
)
Roadmap for ADO.NET
Article ID: 310371 - Last Review: February 27, 2007 - Revision: 2.7
APPLIES TO
- Microsoft ADO.NET 2.0
- Microsoft ADO.NET (included with the .NET Framework)
- Microsoft Visual Basic 2005
- Microsoft Visual Basic .NET 2002 Standard Edition
- Microsoft Visual Basic .NET 2003 Standard Edition
| kbprb kbsystemdata KB310371 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate