Saving Dataview to a Dataset

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>
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

The problem y ou have is with FKs apparently.

Another thing, a DataView a related to a DataTable, not to a DataSet.

So you DON'T want to clone the dataset, not even clone the table will works
always.

If you have a column that makes reference to anther table you may have a
problem.

You have to call DataTable.Clone and then iterate in the DataView.Rows to
copy them.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Dennis said:
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>
 
B

bblBrox_Zaphod

There are a couple of things you can try. What's happening here is
that you are attempting to copy a constrained table into a data set
that does not have the external references. i.e. your table "parts"
has a column constraint that "parts.partSupplier" must be a key in
"PartSuppliers" By just iterating over the tables collection, you run
the risk of copying "parts" before "partssuppliers," so you get the
constraints.

you might try setting the DataSet.EnforceConstraints propertyy false on
the target DataSet. If you then run the transfer, you might copy all
the tables over, but none of the constraints.

You might also need to explicitly export the relations, if you want
them, into the target DataSet. (See the DataSet.Relations property).

Jim Katz
Senior Developer
Transform Pharmaceuticals
(e-mail address removed)
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top