Solved

AutoIncrement With master detail relationship

Posted on 2006-11-15
9
536 Views
Last Modified: 2008-01-09
I Have a Master Detail relationship where the Parent PK is an AutoIncrement field.

I am using a ADO 2.0 Dataset and i set the DataColumn's AutoIncrement and seed settings to -1, -1 so each new row gets added with a fake negative ID.

I then add a new Parent row and it gets an ID of -1, I then add Child row(s) and set the parent Id to -1.

I then attempt to save by calling the the dataAdapter.update  (first on the parent and then on the child).

the parent row gets the new ID (the SQL command refershes the Data correctly) and the child row is even updated with the correct parent Id that was generated by the SQL server.

the problems now begin....

If i set the adapter.AcceptChangesDuringUpdate = true (before saving the parent table).

I call the adapter.update for the child table and It does not see any updated rows in the child table.

If i set the adapter.AcceptChangesDuringUpdate = false (before saving the parent table).

I call the adapter.update for the child table and it runs the Update command instead of the Insert command.

It seems the Update of the parent is changing the RowState of the child (the parentID is updated in the child row by the adapter), however since the child rowstate is added, any update to it should not change it's row state. (this row was never saved to the database, so any updates prior to the save it should remain in state added).

0
Comment
Question by:ANAT2403
  • 4
  • 3
9 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 17955860
<adapter.AcceptChangesDuringUpdate = true >

without doing this cant u try AcceptChanges() method

after both the updates
0
 

Author Comment

by:ANAT2403
ID: 17956300
The first update (parent table) messes up the rowstate of the child table, once that happened i lost the rowstate info in the child rows.
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 17956515
adapter.AcceptChangesDuringUpdate = false

master.update();
child.update();
master.AcceptChanges();
Child.AcceprtCahnges();
0
 

Author Comment

by:ANAT2403
ID: 17959768
That is what i do but....

If i set the adapter.AcceptChangesDuringUpdate = false (before saving the parent table).

I call the adapter.update for the child table and it runs the Update command instead of the Insert command.

It seems the Update of the parent is changing the RowState of the child (the parentID is updated in the child row by the adapter), however since the child rowstate is added, any update to it should not change it's row state. (this row was never saved to the database, so any updates prior to the save it should remain in state
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 17962889
are u sure the row state is (Modified) not added
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 17962899
how did u come to this conclusion did u put a break point and saw the child row's row-state

if that is the case one more query how are u populationg data is it by user entry or some other way


one more thing can u put a break point before calling the update of the parent and see waht the row state is
0
 

Author Comment

by:ANAT2403
ID: 17974456
Here is the solution, there are a few Microsoft features to workaround.

http://support.microsoft.com/kb/320301


1)Create a new DataAdapter based on the original one to prevent the
   CommandBuilder from modifying the SQL statements,
   specifically the custom InsertCommand.
   You do not need this if you roll your own commands and parameters
   or if you use the Visual Tools to do it.



2)Use a delegate to prevent AcceptChanges from occurring on Deletes and Inserts.
   This is for a limitation of the DataAdapter; see Q313540.
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 18171221
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now