Saving a dataview to a new dataset in C#

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

Cor Ligthert [MVP]

Dennis,

I typed it in this message, so watch typos or other errors.

\\\
DataTable dtnew = dt.Clone();
foreach (DataRowView dvr in dt.DefaultView)
dtnew.ImportRow(dvr.Row);
ds.Tables.Add(dtnew);
///

I hope this helps,

Cor
 
D

Dennis

Hi Cor, that you so much for the help. I made it work using one slight
change. When I made the original Dataset I assigned this directly to
my data grid and I didn't make a data table. I made a Dataview and did
all of my changes there and then assigned this back to my datagrid
leaving the original Dataset unchanged. What I did was use your code
but I created a second Dataset and did all of my changes there thus
leaving the original Dataset unchanged otherwise it was returning the
original table.

Works great and does exactly what I wanted it to do. Thanks again for
the help and I included what I changed in case it helps anyone else
out in the future.

Thanks again, Dennis

//******************************************************

// Creates data table from data set
DataTable dt = ds.Tables[0];

// Clones data table to temp table
DataTable dtnew = dt.Clone();

foreach (DataRowView dvr in dt.DefaultView)
dtnew.ImportRow(dvr.Row);

ds1.Tables.Add(dtnew);

ds1.WriteXml(@"C:\NewTable.xml");
 

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