Microsoft Knowledge Base Email Alertz

KBAlertz.com: (318048) - 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...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 318048 - Last Review: September 4, 2003 - Revision: 2.2

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

This article was previously published under Q318048

On This Page

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.

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 C# .NET syntax
  • ADO.NET syntax
  • Windows Forms

Create the Project

  1. Open Visual Studio .NET.
  2. Create a new Windows application in Visual C# .NET. Form1 is added to the project by default.
  3. Place a Command button on Form1. Change the Name property of the button to btnTest.
  4. Use the using statement on the System.Data.SqlClient 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:
    using System.Data.SqlClient;
  5. Create a typed DataSet using the Customers table in the Northwind database.
  6. Paste the following code in the btnTest_Click event:
    sqlDataAdapter1.Fill(dataSet11,"customers");
    foreach(DataSet1.CustomersRow crow in dataSet11.Customers)
    {
       if (crow.Region == "-1")
    	Console.WriteLine("DBNULL");
    }
  7. Save your project.

Test the Project

  1. 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.
  2. Stop the program and open the DataSet1.xsd file from Solution Explorer.
  3. In the DataSet1.xsd file, 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="urn:schemas-microsoft-com:xml-msprop"
  4. Modify the element Region to reflect the following code:
    <xs:element name="Region" codegen:nullValue="-1" type="xs:string" minOccurs="0" />
  5. Save the DataSet1.xsd file. If you are prompted to reload the file, click Yes.
  6. Run the project and then click btnTest. The exception does not occur. Note that the DBNULLs that are printed in the output window.

Pitfalls

  • The nullValue must match the type of the element; for example, use nullValue="0" to return 0 for null integer fields.
  • The nullValue is only for retrieving data. You cannot use the nullValue to assign to a field.

REFERENCES

Using Annotations with a Typed DataSet (http://msdn.microsoft.com/en-us/library/w2wcdz8t.aspx)


APPLIES TO
  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft ADO.NET 1.1
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
Keywords: 
kbhowtomaster KB318048
       

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

Anonymous User Report As Irrelevant  
Written: 4/24/2004 6:27 PM
Doesn't seem to work with WebServices, however??

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please