O'Reilly logo

ADO.NET Cookbook by Bill Hamilton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

2.19. Replacing Null Values in a Strongly Typed DataSet

Problem

When a column in a database has a null value, you want the value in the DataSet to be a string indicating that no value is available.

Solution

Use annotations in the XML schema to control the handling of null values.

The sample uses one XSD file:

CategoriesDS_AnnotatedNull.xsd

The schema used to generate the strongly typed DataSet. The schema is annotated so the null Description values are replaced with the string "- no description available -“. The annotations are marked in bold in Example 2-25.

Example 2-25. File: TypedDataSets\CategoriesDS_AnnotatedNull.xsd

<?xml version="1.0" standalone="yes" ?>
<xs:schema id="CategoriesDS_AnnotatedNull"
    targetNamespace=
    "http://www.tempuri.org/CategoriesDS_AnnotatedNull.xsd"
    xmlns:mstns="http://www.tempuri.org/CategoriesDS_AnnotatedNull.xsd"
    xmlns="http://www.tempuri.org/CategoriesDS_AnnotatedNull.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
    xmlns:codegen="urn:schemas-microsoft-com:xml-msprop" attributeFormDefault="qualified" elementFormDefault="qualified"> <xs:element name="CategoriesDS_AnnotatedNull" msdata:IsDataSet="true"> <xs:complexType> <xs:choice maxOccurs="unbounded"> <xs:element name="Categories"> <xs:complexType> <xs:sequence> <xs:element name="CategoryID" msdata:ReadOnly="true" msdata:AutoIncrement="true" type="xs:int" /> <xs:element name="CategoryName" type="xs:string" /> <xs:element name="Description" type="xs:string" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required