Microsoft KB Archive/316820

= FIX: Cannot derive and extend a column in XSD with DataSet =

Article ID: 316820

Article Last Modified on 3/10/2006

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft Visual Studio .NET 2002 Professional Edition

-



This article was previously published under Q316820



This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Data



SYMPTOMS
When you try to read an Extensible Markup Language (XML) schema into a DataSet, the following duplicate name exception may be thrown when the XML Schema Definition language (XSD) derives and extends a column definition:

An unhandled exception of type 'System.Data.DuplicateNameException' occurred in system.data.dll

Additional information: A column named 'column1' already belongs to this DataTable.

If you handle this exception within a try/catch block, you receive the following error message:

System.Data.DuplicateNameException: A column named 'column1' already belongs to this DataTable.



CAUSE
The DataSet is treating the column derivation and extension as if it were the definition of an entirely new column, with the same name as an existing column.



RESOLUTION
To avoid this exception, define a new element type with the attributes that you want, instead of deriving from and extending an existing element type.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section. This bug was corrected in Microsoft ADO.NET (included with the .NET Framework 1.1), and Microsoft Visual Studio .NET 2003, Professional Edition.



Steps to reproduce the behavior
  Create a new XSD file named Test.xsd. Paste the following code into it:  

    

<xsd:element name=&quot;table1&quot;>  <xsd:complexContent> <xsd:extension base=&quot;columns&quot;>  <xsd:element name=&quot;column1&quot; msdata:ReadOnly=&quot;True&quot; type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element>

</xsd:schema> </li> <li>Create a new Visual Basic .NET console application.</li> <li> Paste the following code into the sub Main of Module1: Dim ds As New DataSet ds.ReadXmlSchema(&quot;C:\Test.xsd&quot;) 'Exception thrown here. Console.Write(&quot;Schema Loaded Successfuly&quot;) Console.Read </li> <li>Change the string that is being passed in the call to ReadXML to reflect the path to the XML file that you created.</li> <li>Press F5 to run and compile the application. You receive the error message listed in the &quot;Symptoms&quot; section.</li> <li> In order to avoid this exception being thrown, replace code in the XSD file with the following code that defines a new element with the attributes that you want, instead of deriving from an existing element: <?xml version=&quot;1.0&quot; standalone=&quot;yes&quot; ?> <xsd:schema id=&quot;MyDataSet&quot; xmlns=&quot;&quot; xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:msdata=&quot;urn:schemas-microsoft-com:xml-msdata&quot;> <xsd:element name=&quot;table1&quot;>   <xsd:element name=&quot;column1&quot; type=&quot;xsd:string&quot; msdata:ReadOnly=&quot;True&quot; /> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </li> <li>Press F5 to run and compile the application. Output to the console confirms that the XSD schema was loaded successfully. Press ENTER to stop the running application, and then close the Console window.</li></ol>

<div class="references_section">