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" ...

Get ADO.NET Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.