473,491 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Saving Dataview to a Dataset

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>

Nov 17 '05 #1
2 5322
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" <no**@none.com> wrote in message
news:fk********************************@4ax.com...
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>

Nov 17 '05 #2
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
jk***@transformpharma.com

Dennis wrote:
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>


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4578
by: Arthur Dzhelali | last post by:
I according to MSDN dataview and dataset are thread safe for read operations, but we run into this scenario. I just would like to see some comments on it. Aplication written in VB.NET it is an...
1
1818
by: Nikhil Patel | last post by:
Hi, I bind a grid to a DataView object. I lose the reference to the underlying DataTable and the DataSet in a postback. Here is the code. Page_Load works fine and I am able to see the rows in...
1
2089
by: Jay Zweedyk | last post by:
Ok I want to filter a dataset into a dataview and be able to reference back to the dataset from the filtered dataview. Example: 100 record dataset filter it to a 5 record dataview loop...
36
4411
by: kjvt | last post by:
Based on a prior posting, I've written a function to convert a recordset to a dataview. The first call to the function for a given recordset works perfectly, but the second call always returns a...
13
2064
by: Steve | last post by:
I have a form with a dataset and a datagrid. I created a dataview on this dataset. When the user modifies the datagrid, I look up this record in the dataview to make sure it is unique. Here is...
5
8809
by: David Wender | last post by:
I want to create a dataview with a sort on multiple columns. However, when I use FindRows, I only want to search some of the columns, not all. Is this possible? I have not been able to make it...
0
2562
by: Bob Davies | last post by:
Hi I have a webservice that retrieves data from a database, this is then returned to the calling client application built in windows forms within a dataset, however upon attempting to create...
17
2728
by: A_PK | last post by:
I have problem databinding the DataGrid with DataView/DataSet after the filter... I create the following proceudre in order for user to filter as many as they want, but the following code is only...
4
1570
by: James | last post by:
Basically I have a DataGrid that I'm binding to the results of a stored procedure call. The recordset is fairly small. Initially I'm creating a DataSet from the results and binding it. There's a...
0
6978
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7154
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7190
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5451
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4881
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4578
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3086
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1392
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.