2.18. Replacing Null Values in a Strongly Typed DataSet

Problem

When a column has a null value, you want the value in the strongly typed 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.

Use the Visual Studio .NET IDE to create the strongly typed DataSet object based on the HumanResources.Department table in AdventureWorks that you will annotate. For more details about creating a strongly typed DataSet using the Visual Studio .NET IDE, see Recipe 2.16. Follow these steps:

  1. Create a Visual C# Console Application, ReplaceNullValueStronglyTypedDataSet.

  2. Right-click on the project in Solution Explorer and click Add → New Item to open the Add New Item dialog shown in the following figure. Select DataSet from the Visual Studio installed templates. Name the DataSet ReplaceNullValueDepartment.xsd. Click the Add button to create the DataSet and close the dialog. The DataSet is added to Solution Explorer and the DataSet Designer is opened.

  3. Next, add database objects to the DataSet using a Database Connection. If you do not already have an AdventureWorks Database Connection, create one: Open Server Explorer by selecting View → Server Explorer from the main menu. Rightclick on the Data Connections node and select Add Connection from the context menu to Open the Add Connection dialog. In the Add Connection dialog, select the SQL Server with the AdventureWorks database, leave log on mode as Windows Authentication, ...

Get ADO.NET 3.5 Cookbook, 2nd Edition 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.