473,399 Members | 3,888 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,399 software developers and data experts.

Q: Parent Child Update Problem

Hi Everybody

I was wondering if anybody could help me with something that has been
bothering me for some time. Basically, it is a problem I encountered some
time ago with DataSets. I found a solution (which I will refer to later) but
I have a gut feeling that there must be an easier way to do it.

In order I can explain the problem, consider the following: Suppose I have a
DataSet which has two tables "Customers" and "Orders". I set up a
relationship between both tables so that a customer can have one or many
orders. This means that "Orders" has a foreign key e.g. FK_Customer which is
copy of the primary key of the corresponding customer row. So, when I add
new orders for a customer, the new rows in the "Orders" table must have the
correct FK_Customer.

This is the problem:

When the "Customer" table is updated to the database, the primary keys of
newly added rows to this table WILL NOT in general be the same when in the
DataSet after being updated. For example, microsoft recommendes that the
primary keys in a disconnected DataSet are negative. This means that when we
come to update the "Orders" table, the values of FK_Customer will not be
correct!

As I mentioned before, I did find a solution to the problem i.e.

http://www.codeproject.com/cs/databa...select=1587944

but this involves telling the child row which is its parent row (via
SetParentRow) and handling the OnRowUpdated event.

Have I been missing something? It occurs to me that this parent-child
scenario is so frequent that there must be an easier way to deal with the
situation. Has VS2005 added any new funcitonality to deal with it?

Thanks in advance

G

Feb 28 '07 #1
3 2398
G .Net wrote:
<snip>
Suppose I have a
DataSet which has two tables "Customers" and "Orders". I set up a
relationship between both tables so that a customer can have one or many
orders. This means that "Orders" has a foreign key e.g. FK_Customer which is
copy of the primary key of the corresponding customer row. So, when I add
new orders for a customer, the new rows in the "Orders" table must have the
correct FK_Customer.

This is the problem:

When the "Customer" table is updated to the database, the primary keys of
newly added rows to this table WILL NOT in general be the same when in the
DataSet after being updated. For example, microsoft recommendes that the
primary keys in a disconnected DataSet are negative. This means that when we
come to update the "Orders" table, the values of FK_Customer will not be
correct!
<snip>

Maybe you could perform an update of the newly created Customer
*before* adding any orders... This would ensure that the Customer
already had the correct PK in place, I guess.

A kludge, I know.

Regards,

Branco.

Feb 28 '07 #2
Hi,

"G .Net" <no********@email.comwrote in message
news:Nr******************************@pipex.net...
Hi Everybody

I was wondering if anybody could help me with something that has been
bothering me for some time. Basically, it is a problem I encountered some
time ago with DataSets. I found a solution (which I will refer to later)
but I have a gut feeling that there must be an easier way to do it.

In order I can explain the problem, consider the following: Suppose I have
a DataSet which has two tables "Customers" and "Orders". I set up a
relationship between both tables so that a customer can have one or many
orders. This means that "Orders" has a foreign key e.g. FK_Customer which
is copy of the primary key of the corresponding customer row. So, when I
add new orders for a customer, the new rows in the "Orders" table must
have the correct FK_Customer.

This is the problem:

When the "Customer" table is updated to the database, the primary keys of
newly added rows to this table WILL NOT in general be the same when in the
DataSet after being updated. For example, microsoft recommendes that the
primary keys in a disconnected DataSet are negative. This means that when
we come to update the "Orders" table, the values of FK_Customer will not
be correct!

As I mentioned before, I did find a solution to the problem i.e.

http://www.codeproject.com/cs/databa...select=1587944

but this involves telling the child row which is its parent row (via
SetParentRow) and
All what SetParentRow does is set the FK to the PK of the parent row, you
could quite easily do this yourself, even though these PK/FK may be
temporary ones (negative) at least the rows are associated which is
important.

When the parent DataRow gets updated it will retrieve the new PK, if there
is a ForeignKeyConstraint between the PK and FK and UpdateRule is set to
Cascade, then the new PK's are propgated to the relevant FK's. Note that
this only works because the rows were associated using the temporary keys.
handling the OnRowUpdated event.
You have to handle OnRowUpdated event to retrieve the DB generated keys.

In VC2005 you have to do this for Access (not sure if it's true for all
OleDB or Jet), but it's even harder then in VC2003, because the TableAdapter
doesn't expose the DataAdapter so you can't attach to the RowUpdating event.
Foreach TableAdapter, you need to add another partial TableAdapter class and
expose the private DataAdapter by adding a property.

In VC2005 you don't need to do this for SQL server, the TableAdapter will
automatically retrieve them if the right checkbox is checked while
configurating the TableAdapter.
>
Have I been missing something? It occurs to me that this parent-child
scenario is so frequent that there must be an easier way to deal with the
It gets more complicated if you consider that updating involves deleted,
modified and added rows, and to avoid integrity problems _during_ update, a
certain update sequence is required, which is explained in the following
article:
http://msdn2.microsoft.com/en-us/library/ms971502.aspx

situation. Has VS2005 added any new funcitonality to deal with it?
No.

HTH,
Greetings
Thanks in advance

G


Feb 28 '07 #3
Thanks guys. Most helpful.

G

"Bart Mermuys" <bm*************@hotmail.comwrote in message
news:YK********************@phobos.telenet-ops.be...
Hi,

"G .Net" <no********@email.comwrote in message
news:Nr******************************@pipex.net...
>Hi Everybody

I was wondering if anybody could help me with something that has been
bothering me for some time. Basically, it is a problem I encountered some
time ago with DataSets. I found a solution (which I will refer to later)
but I have a gut feeling that there must be an easier way to do it.

In order I can explain the problem, consider the following: Suppose I
have a DataSet which has two tables "Customers" and "Orders". I set up a
relationship between both tables so that a customer can have one or many
orders. This means that "Orders" has a foreign key e.g. FK_Customer which
is copy of the primary key of the corresponding customer row. So, when I
add new orders for a customer, the new rows in the "Orders" table must
have the correct FK_Customer.

This is the problem:

When the "Customer" table is updated to the database, the primary keys of
newly added rows to this table WILL NOT in general be the same when in
the DataSet after being updated. For example, microsoft recommendes that
the primary keys in a disconnected DataSet are negative. This means that
when we come to update the "Orders" table, the values of FK_Customer will
not be correct!

As I mentioned before, I did find a solution to the problem i.e.

http://www.codeproject.com/cs/databa...select=1587944

but this involves telling the child row which is its parent row (via
SetParentRow) and

All what SetParentRow does is set the FK to the PK of the parent row, you
could quite easily do this yourself, even though these PK/FK may be
temporary ones (negative) at least the rows are associated which is
important.

When the parent DataRow gets updated it will retrieve the new PK, if there
is a ForeignKeyConstraint between the PK and FK and UpdateRule is set to
Cascade, then the new PK's are propgated to the relevant FK's. Note that
this only works because the rows were associated using the temporary keys.
>handling the OnRowUpdated event.

You have to handle OnRowUpdated event to retrieve the DB generated keys.

In VC2005 you have to do this for Access (not sure if it's true for all
OleDB or Jet), but it's even harder then in VC2003, because the
TableAdapter doesn't expose the DataAdapter so you can't attach to the
RowUpdating event. Foreach TableAdapter, you need to add another partial
TableAdapter class and expose the private DataAdapter by adding a
property.

In VC2005 you don't need to do this for SQL server, the TableAdapter will
automatically retrieve them if the right checkbox is checked while
configurating the TableAdapter.
>>
Have I been missing something? It occurs to me that this parent-child
scenario is so frequent that there must be an easier way to deal with the

It gets more complicated if you consider that updating involves deleted,
modified and added rows, and to avoid integrity problems _during_ update,
a certain update sequence is required, which is explained in the following
article:
http://msdn2.microsoft.com/en-us/library/ms971502.aspx

>situation. Has VS2005 added any new funcitonality to deal with it?

No.

HTH,
Greetings
>Thanks in advance

G



Mar 2 '07 #4

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

Similar topics

2
by: Raj | last post by:
Hi All, I have a problem with trying to refresh the parent window from child window in order to update data in the parent window. The sequence of events are 1) I click a button in the parent...
1
by: Roy | last post by:
How is it done? BTW, I'm reposting this here, as the datagrid newsgroup appears functionally comatose... I have a nested datagrid setup. When one updates the child grid using edit, it also...
1
by: Bill Borg | last post by:
Hello all, Simple chat app, where the site owner has a master window with all requests for chat, status of each room, etc., and child windows for each separate chat in which the owner is...
1
by: Mr Newbie | last post by:
Sorry to bother you guys but I have another questions related to DataSets. I'm almost there now, so I dont expect to bug you much more ( Hopefully ). I have Master / Details tables with...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
1
by: Aaron Smith | last post by:
I have a parent table that has one child table. The parent has a single field (ID, AutoIncrement, Seed 0, Step -1 in the DataSet, Seed 1, step 1, in DataSource). The child is linked to this ID...
3
by: PAUL | last post by:
Hello, I have 2 datasets I am trying to update. The parent table seems to update fine but when I go update the chiled table I get an error message that says I need a related record in the parent...
18
by: Chris Ianson | last post by:
Hi geniuses (or is that genii, or genies) The challenge is as above really. I have a page with an iframe in it, and need to call a JS function in the *parent* page, *from* inside the iframe. ...
1
by: Hexman | last post by:
Hello All, What I'm trying to do is update a child record using a parent-child relation. I want to find out if it is faster than than doing multiple selects. Anyways, I've created a dataset...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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
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
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,...

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.