Removing Dataset Column when DS has schema.

  • Thread starter Thread starter Mark Myers
  • Start date Start date
M

Mark Myers

Question: If I populate a dataset using an xsd and an xml file, can I
then remove a column and still reference the data? I get an error when I
try it.
Note1: if I don't use the xsd it's OK. What gives?
Note2: calling Update on an SQL data adapter gives the same error.
Note3: calling AcceptChanges() gives the same error.

Sample code:
DataSet ds = new DataSet();
// read xml schema into dataset
ds.ReadXmlSchema("Test1.xsd");
// read xml into dataset
ds.ReadXml("Test1.xml");
// Remove a column
ds.Tables["Data"].Columns.Remove("UselessInfo");
// Error occurs here ...
ds.Tables["Data"].Rows[0]["Hobby"] = "Flying";

Error:
System.IndexOutOfRangeException
Cannot find column 2

StackTrace:
at System.Data.DataColumnCollection.get_Item(Int32 index)
at System.Data.Index.CompareRecords(Int32 record1, Int32 record2)
at System.Data.Index.GetIndex(Int32 record)
at System.Data.Index.RecordStateChanged(Int32 oldRecord,
DataViewRowState oldOldState, DataViewRowState oldNewState, Int32
newRecord, DataViewRowState newOldState, DataViewRowState newNewState)
at System.Data.DataTable.RecordStateChanged(Int32 record1,
DataViewRowState oldState1, DataViewRowState newState1, Int32 record2,
DataViewRowState oldState2, DataViewRowState newState2)
at System.Data.DataTable.SetNewRecord(DataRow row, Int32 proposedRecord,
DataRowAction action, Boolean isInMerge)
at System.Data.DataRow.SetNewRecord(Int32 record)
at System.Data.DataRow.EndEdit()
at System.Data.DataRow.set_Item(DataColumn column, Object value)
at System.Data.DataRow.set_Item(String columnName, Object value)

Xsd:
<?xml version="1.0" standalone="yes"?>
<xs:schema xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" id="Import">
<xs:element name="Import" msdata:IsDataSet="true" msdata:Locale="en-
GB">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Data" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Hobby" type="xs:string"/>
<xs:element name="UselessInfo" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="DataPK" msdata:PrimaryKey="true">
<xs:selector xpath=".//Data"/>
<xs:field xpath="Name"/>
</xs:unique>
</xs:element>
</xs:schema>

Xml:
<?xml version="1.0" encoding="utf-8"?>
<Import>
<Data>
<Name>Me</Name>
<Hobby>Crying</Hobby>
<UselessInfo>Fishing is boring</UselessInfo>
</Data>
<Data>
<Name>Bob</Name>
<Hobby>Making me play with this stuff</Hobby>
<UselessInfo>Whatever</UselessInfo>
</Data>
<Data>
<Name>Jackie</Name>
<Hobby>Skydiving</Hobby>
<UselessInfo>etc</UselessInfo>
</Data>
</Import>
 
I blinked, and Mark Myers said on 15 Oct 2003 ...
Question: If I populate a dataset using an xsd and an xml file, can I
then remove a column and still reference the data? I get an error when I
try it.
Note1: if I don't use the xsd it's OK. What gives?
Note2: calling Update on an SQL data adapter gives the same error.
Note3: calling AcceptChanges() gives the same error.

Sample code:
[snipped]

No ideas then? Is this a bug?
 
Back
Top