Microsoft KB Archive/318039

= HOW TO: Make a Typed DataSet Return a Default Value Instead of DBNull by Using Visual Basic .NET =

Article ID: 318039

Article Last Modified on 9/4/2003

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft ADO.NET 1.1
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



This article was previously published under Q318039



For a Microsoft Visual C# .NET version of this article, see 318048.

IN THIS TASK
SUMMARY
 * Requirements
 * Create the Project
 * Test the Project
 * Pitfalls

REFERENCES



SUMMARY
This step-by-step article describes how to change the XML Schema file to make a typed DataSet return a default value instead of DBNULL.

Visual Studio .NET may return an exception if the value of a DataRow is DBNULL, so you must handle the exception properly for values that are DBNULL. If you modify the default value for DBNULL fields, you can retrieve a value you specify other than DBNULL.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows XP, Windows 2000, or Windows NT 4.0 Service Pack 6a
 * Microsoft Data Access Components (MDAC) 2.6 or later
 * Microsoft Visual Studio .NET

This article assumes that you are familiar with the following topics:
 * Visual Basic .NET syntax
 * ADO.NET syntax
 * Windows Forms

back to the top

Create the Project
 Open Visual Studio .NET. Create a new Windows application in Visual Basic .NET. Form1 is added to the project by default. Place a Command button on Form1. Change the Name property of the button to btnTest .  Use the Imports statement on the System and System.Data namespaces 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  Use the Customers table in the Northwind database to create a typed DataSet.  Paste the following code in the btnTest_Click event: SqlDataAdapter1.Fill(DataSet11, &quot;customers&quot;) Dim crow As DataSet1.CustomersRow For Each crow In DataSet11.Customers If crow._Region = &quot;-1&quot; Then Console.WriteLine(&quot;DBNULL&quot;) End If   Next  Save your project.</li></ol>

back to the top

Test the Project
<ol> Run the project and then click btnTest. The following error message appears.

An unhandled exception of type 'System.Data.StrongTypingException' occurred in nullvaluedbnull.exe

Additional information: Cannot get value because it is DBNull.

</li> Stop the program and open the DataSet1.xsd file from Solution Explorer.</li>  In the DataSet1.xsd, add the namespace codegen in the namespace declaration. Paste the following line of code for the codegen namespace immediately after the targetNamespace value: xmlns:codegen=&quot;urn:schemas-microsoft-com:xml-msprop&quot; </li>  Modify the element Region to reflect the following code sample: <xs:element name=&quot;Region&quot; codegen:nullValue=&quot;-1&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; /> </li> Save the DataSet1.xsd file. If you are prompted to reload the file, click Yes.</li> Run the project and then click btnTest. The exception does not occur. Notice the DBNULLs that are printed in the output window.</li></ol>

back to the top

Pitfalls

 * The nullValue must match the type of the element; for example, use nullValue=&quot;0&quot; to return 0 for null integer fields.
 * The nullValue only retrieves data. You cannot use the nullValue to assign to a field.

back to the top