473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

DataSet Relation Exception Issue

I have a situation where I need to add rows to tables with a Parent-Child
relationship which presents a constraints violation when I reject the
changes. Here is the scenario:

I add a row to a Parent table which then causes a row to be added to the
Child table referencing the parent row. The Child table rows can be modified
by multiple processes so I need to manage row additionals separate from row
modifications in terms of how they are canceled or rejected. When I need to
cancel the changes for the child added rows I step through and identify the
added rows and RejectChanges() for each. I can do the same process for the
parent table added rows. However, if I RejectChanges() for the Parent table
as a whole I get the following error:
"Cannot make this change because constraints are enforced on relation
ParentToChild, and changing this value will strand child rows."

The Child table added rows have all been removed prior to the Parent table
RejectChanges() call so it doesn't make any sense to me. I've assemble the
following example to illustrate the problem. Is this a .NET bug or am I doing
something incorrectly here.

public void TestProblem()
{
int intKey = 1;

// Create DataSet with Parent and Child tables
DataSet ds = CreateDataSet();

// Add Parent and Child to tables
AddRow( ds.Tables["Parent"], intKey );
AddRow( ds.Tables["Child"], intKey );
ds.AcceptChanges();

// Add another Child to first parent
AddRow( ds.Tables["Child"], intKey );
// Add another Parent and Child to tables
AddRow( ds.Tables["Parent"], ++intKey );
AddRow( ds.Tables["Child"], intKey );

// Roll back added Child rows
RollBackAdded( ds.Tables["Child"] );
// Roll back added Parent rows
RollBackAdded( ds.Tables["Parent"] );

// Roll back added Parent rows
ds.Tables["Parent"].RejectChanges();
}

public DataSet CreateDataSet()
{
DataSet ds = new DataSet();

// Create Parent Table
DataTable dtParent = new DataTable("Parent");
DataColumn dc = dtParent.Columns.Add("key", typeof(Int32));
dc.AllowDBNull = false;
dc.Unique = true;
dtParent.Columns.Add("Name", typeof(String));
ds.Tables.Add(dtParent);

// Create Child Table
DataTable dtChild = new DataTable("Child");
dc = dtChild.Columns.Add("key", typeof(Int32));
dc.AllowDBNull = false;
dtChild.Columns.Add("Name", typeof(String));
ds.Tables.Add(dtChild);

ds.Relations.Add("ParentToChild", dtParent.Columns["key"],
dtChild.Columns["key"] );
return ds;
}

public DataRow AddRow( DataTable dt, int key )
{
DataRow dr = dt.NewRow();
dr["key"] = key;
dr["Name"] = dt.TableName + " " + key.ToString();
dt.Rows.Add(dr);
dr.EndEdit();
return dr;
}

public void RollBackAdded( DataTable dt )
{
// Roll back Added or Deleted rows
DataTable dtChanged = dt.GetChanges();
if( dtChanged != null )
{
DataRow [] dra = new DataRow[dtChanged.Rows.Count];
int no = 0;
foreach( DataRow dr in dtChanged.Rows )
if( dr.RowState == DataRowState.Added || dr.RowState ==
DataRowState.Deleted )
dra[no++] = dr;

for( int jo = 0; jo < no; jo++ )
dra[jo].RejectChanges();
}
}
Nov 16 '05 #1
1 8557
Well, once again, going through the process of documenting my issue I found
the solution. The problem was that I was using the GetChanges() for the
tables which was returning a copy of the affected rows. Rejecting the changes
on these rows did not affect the rows in the original DataSet. Once I removed
the use of this method and resorted to searching the original tables for the
affected rows and applying the RejectChanges() to them the exception went
away. All is well.

"Fleckman" wrote:
I have a situation where I need to add rows to tables with a Parent-Child
relationship which presents a constraints violation when I reject the
changes. Here is the scenario:

I add a row to a Parent table which then causes a row to be added to the
Child table referencing the parent row. The Child table rows can be modified
by multiple processes so I need to manage row additionals separate from row
modifications in terms of how they are canceled or rejected. When I need to
cancel the changes for the child added rows I step through and identify the
added rows and RejectChanges() for each. I can do the same process for the
parent table added rows. However, if I RejectChanges() for the Parent table
as a whole I get the following error:
"Cannot make this change because constraints are enforced on relation
ParentToChild, and changing this value will strand child rows."

The Child table added rows have all been removed prior to the Parent table
RejectChanges() call so it doesn't make any sense to me. I've assemble the
following example to illustrate the problem. Is this a .NET bug or am I doing
something incorrectly here.

public void TestProblem()
{
int intKey = 1;

// Create DataSet with Parent and Child tables
DataSet ds = CreateDataSet();

// Add Parent and Child to tables
AddRow( ds.Tables["Parent"], intKey );
AddRow( ds.Tables["Child"], intKey );
ds.AcceptChanges();

// Add another Child to first parent
AddRow( ds.Tables["Child"], intKey );
// Add another Parent and Child to tables
AddRow( ds.Tables["Parent"], ++intKey );
AddRow( ds.Tables["Child"], intKey );

// Roll back added Child rows
RollBackAdded( ds.Tables["Child"] );
// Roll back added Parent rows
RollBackAdded( ds.Tables["Parent"] );

// Roll back added Parent rows
ds.Tables["Parent"].RejectChanges();
}

public DataSet CreateDataSet()
{
DataSet ds = new DataSet();

// Create Parent Table
DataTable dtParent = new DataTable("Parent");
DataColumn dc = dtParent.Columns.Add("key", typeof(Int32));
dc.AllowDBNull = false;
dc.Unique = true;
dtParent.Columns.Add("Name", typeof(String));
ds.Tables.Add(dtParent);

// Create Child Table
DataTable dtChild = new DataTable("Child");
dc = dtChild.Columns.Add("key", typeof(Int32));
dc.AllowDBNull = false;
dtChild.Columns.Add("Name", typeof(String));
ds.Tables.Add(dtChild);

ds.Relations.Add("ParentToChild", dtParent.Columns["key"],
dtChild.Columns["key"] );
return ds;
}

public DataRow AddRow( DataTable dt, int key )
{
DataRow dr = dt.NewRow();
dr["key"] = key;
dr["Name"] = dt.TableName + " " + key.ToString();
dt.Rows.Add(dr);
dr.EndEdit();
return dr;
}

public void RollBackAdded( DataTable dt )
{
// Roll back Added or Deleted rows
DataTable dtChanged = dt.GetChanges();
if( dtChanged != null )
{
DataRow [] dra = new DataRow[dtChanged.Rows.Count];
int no = 0;
foreach( DataRow dr in dtChanged.Rows )
if( dr.RowState == DataRowState.Added || dr.RowState ==
DataRowState.Deleted )
dra[no++] = dr;

for( int jo = 0; jo < no; jo++ )
dra[jo].RejectChanges();
}
}

Nov 16 '05 #2

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

Similar topics

3
by: Jane | last post by:
Hello, I am working on a windows application with C#. What I need is to display a list of data in 2nd combo box based on the selection of the 1st combo box. I used SqlCommand object and...
1
by: bitwxtadpl | last post by:
-First I add and relate the tables A and B. Create a new instance of TableA Add TableA to DataSetX Create a new instance of TableB Add TableB to DataSetX Create a relation between TableA and...
5
by: Corno | last post by:
Hi all, If I want to provide a typed dataset from a webservice and if that dataset has relations that are nested (isNested=True), then the relations(keyrefs) are not available in the XSD that is...
0
by: peter mpa | last post by:
Greetings. I am building a c# windows forms app to show tables from a ms access database. I am using OleDbConnection with Microsoft.Jet.OLEDB.4.0 , 5 datagrids and some data relations to show on...
4
by: Robert Bravery | last post by:
Hi all, I have now correctly set up my dataset and two grids, so that the parent navigates the child. THe thing is that the child table is actually a lookup type table. It lists billing types for...
2
by: Joe | last post by:
Hi I have a dataset with 3 tables and 2 relations Is there a way to when I am at 1 row to tell if there is a relation on that row ??? I have the code hardcoded but try to make it work if the #...
1
by: Computer | last post by:
I brought an XSD into the XMLDesigner. The XSD had two related tables and it looked like this in .Net's XMLDesigner: Table 1 --------- BankItem (Bank Item)
0
by: ~J~ | last post by:
Hi, I really hope someone can help me out here. In relation to this thread ( http://www.thescripts.com/forum/thread349557.html ) I'm having a similar issue and getting nowhere fast! In a...
0
by: Ohad Weiss | last post by:
Hi all, I've once asked about that topic. but didn't get an answer. I have a dataset based on 4 tables, which have relation between them. The main table presented to the user on textboxes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.