D
Dennis
Hi, I am hoping I can get some help with a small problem I have run
into using C#. U have an XML file that I load into a Dataset and then
display this in a Datagrid. No problems doing this at all. I then use
a Dataview to filter this view using a keyword and I have no problems
with this either.
What I would like to do is to save this Dataview to a new Dataset so I
can save the reslts to a new file but here is where I am having the
problem. A Dataview is typically not written back to a Dataset so I'm
not sure what would be the best way to do this. I found a code snippet
after looking for a day but I have errors with it and I do not know
why.
I have created my main Dataset called 'ds' and after filtering it and
displaying it I clone 'ds' to a new Dataset and then try to import the
rows back into my new data set. The code I am using to do this is
posted below....
//***************************************************
DataSet ds2 = ds.Clone();
for(int iTable = 0; iTable < ds.Tables.Count; iTable++)
{
for(int iRow = 0; iRow < ds.Tables[iTable].DefaultView.Count; Row++)
{
ds2.Tables[iTable].ImportRow(ds.Tables[iTable].DefaultView[iRow].Row);
}
}
ds2.WriteXml(@"c:\data.xml");
//*************************************************
My problem comes in on the this line...
for(int iRow = 0; iRow < ds.Tables[iTable].DefaultView.Count; iRow++)
The error that is displayed is as follows....
"An unhandled exception of type
'System.Data.InvalidConstraintException' occurred in system.data.dll
Additional information: ForeignKeyConstraint Computer_ComputerHardware
requires the child key values (3) to exist in the parent table."
On this part of the code three matches are found & displayed so I'm
not sure if it is important.
The error occurs on the 'iRow++' part pf the statement but I don't
know why. I can't set breakpoints in C# nor can I see where the error
is because I can't step through the code. Any idea of this would
happen? I'm really stumped on this, below is a snippet of my XML
document so you know what I am using in this code.
Thanks for any help, Dennis
//*****************************************
<Office_Inventory>
<Computer>
<RoomNumber>G2017</RoomNumber>
<ComputerNumber>G201701</ComputerNumber>
<InServiceDate>2003-03-15</InServiceDate>
<ComputerHardware>
<Printer>HP DeskJet 500</Printer>
<Scanner>HP Scan 100</Scanner>
<CD-DVD>40X CD-ROM</CD-DVD>
<Monitor>Acer 67L</Monitor>
</ComputerHardware>
<ComputerSoftware>
<OS>Windows 2000</OS>
<Office>Office 2003 Professional</Office>
<Programming>Visual Studio 2003 Profressional</Programming>
<Utilities>Norton AntiVirus 2005</Utilities>
</ComputerSoftware>
</Computer>
</Office_Inventory>
into using C#. U have an XML file that I load into a Dataset and then
display this in a Datagrid. No problems doing this at all. I then use
a Dataview to filter this view using a keyword and I have no problems
with this either.
What I would like to do is to save this Dataview to a new Dataset so I
can save the reslts to a new file but here is where I am having the
problem. A Dataview is typically not written back to a Dataset so I'm
not sure what would be the best way to do this. I found a code snippet
after looking for a day but I have errors with it and I do not know
why.
I have created my main Dataset called 'ds' and after filtering it and
displaying it I clone 'ds' to a new Dataset and then try to import the
rows back into my new data set. The code I am using to do this is
posted below....
//***************************************************
DataSet ds2 = ds.Clone();
for(int iTable = 0; iTable < ds.Tables.Count; iTable++)
{
for(int iRow = 0; iRow < ds.Tables[iTable].DefaultView.Count; Row++)
{
ds2.Tables[iTable].ImportRow(ds.Tables[iTable].DefaultView[iRow].Row);
}
}
ds2.WriteXml(@"c:\data.xml");
//*************************************************
My problem comes in on the this line...
for(int iRow = 0; iRow < ds.Tables[iTable].DefaultView.Count; iRow++)
The error that is displayed is as follows....
"An unhandled exception of type
'System.Data.InvalidConstraintException' occurred in system.data.dll
Additional information: ForeignKeyConstraint Computer_ComputerHardware
requires the child key values (3) to exist in the parent table."
On this part of the code three matches are found & displayed so I'm
not sure if it is important.
The error occurs on the 'iRow++' part pf the statement but I don't
know why. I can't set breakpoints in C# nor can I see where the error
is because I can't step through the code. Any idea of this would
happen? I'm really stumped on this, below is a snippet of my XML
document so you know what I am using in this code.
Thanks for any help, Dennis
//*****************************************
<Office_Inventory>
<Computer>
<RoomNumber>G2017</RoomNumber>
<ComputerNumber>G201701</ComputerNumber>
<InServiceDate>2003-03-15</InServiceDate>
<ComputerHardware>
<Printer>HP DeskJet 500</Printer>
<Scanner>HP Scan 100</Scanner>
<CD-DVD>40X CD-ROM</CD-DVD>
<Monitor>Acer 67L</Monitor>
</ComputerHardware>
<ComputerSoftware>
<OS>Windows 2000</OS>
<Office>Office 2003 Professional</Office>
<Programming>Visual Studio 2003 Profressional</Programming>
<Utilities>Norton AntiVirus 2005</Utilities>
</ComputerSoftware>
</Computer>
</Office_Inventory>