Microsoft KB Archive/320881

= FIX: DataSet Containing Attribute Column and Default Value Generates Exception When AllowDBNull Set to False =

Article ID: 320881

Article Last Modified on 9/18/2003

-

APPLIES TO


 * Microsoft .NET Framework 1.0

-



This article was previously published under Q320881



SYMPTOMS
When you try to load a DataSet that contains a combination of an attribute column and a default value, and AllowDBNull is set to false, System.Xml.Schema.Compiler generates an XmlSchemaException exception.

For example, if you serialize the following DataSet to XML dc = dt.Columns.Add(&quot;c2&quot;, System.Type.GetType(&quot;System.String&quot;)); dc.AllowDBNull = false; dc.DefaultValue = &quot;Hello&quot;; dc.ColumnMapping = MappingType.Attribute; the DataSet generates an XML schema that contains the following definition:      According to the World Wide Web Consortium (W3C) XML schema specification, the use attribute must be optional or absent if the default attribute is present. Therefore, the schema that the DataSet generates is not valid.



RESOLUTION
To work around this problem, either set the column mapping as an element instead of as an attribute, or do not set the default value.



STATUS
This bug was corrected in Microsoft .NET Framework Class Libraries.



Steps to Reproduce the Behavior
  In Microsoft Visual Studio .NET, create a Visual C# Console Application project. Replace the code in Class1.cs with the following: using System; using System.Data; using System.Xml; using System.Xml.Schema;

namespace ConsoleApplication1 {  class Class1 {     [STAThread] static void Main(string[] args) {        try {           DataSet ds = new DataSet; DataTable dt = ds.Tables.Add(&quot;Table1&quot;); DataColumn dc;

dc = dt.Columns.Add(&quot;c2&quot;, System.Type.GetType(&quot;System.String&quot;)); dc.AllowDBNull = false; // Workaround 1: Comment the following line to avoid the problem (having no default value). dc.DefaultValue = &quot;Hello&quot;; dc.ColumnMapping = MappingType.Attribute; // Workaround 2: Set the ColumnMapping to element as shown below to avoid the problem. // dc.ColumnMapping = MappingType.Element;

ds.AcceptChanges; ds.WriteXml(&quot;dataset.xml&quot;, XmlWriteMode.WriteSchema);

DataSet ds2 = new DataSet; ds2.ReadXml(&quot;dataset.xml&quot;); }        catch(XmlSchemaException ex) {           Console.WriteLine(ex.Message); }        Console.Read; }  }

}  Compile the code, and then run the code in Debug mode. You receive the following error message:

The 'use' attribute must be optional (or absent) if the default attribute is present.

 Try one of the workarounds that are described in the inline comments. If you set no default value or if you set the column mapping as an element, you do not receive the exception error message.</li></ol>

Keywords: kbbug kbpending KB320881

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.