Link to home
Start Free TrialLog in
Avatar of ANAT2403
ANAT2403Flag for Israel

asked on

AutoIncrement With master detail relationship

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).

Avatar of Gautham Janardhan
Gautham Janardhan

<adapter.AcceptChangesDuringUpdate = true >

without doing this cant u try AcceptChanges() method

after both the updates
Avatar of ANAT2403

ASKER

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.
adapter.AcceptChangesDuringUpdate = false

master.update();
child.update();
master.AcceptChanges();
Child.AcceprtCahnges();
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
are u sure the row state is (Modified) not added
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
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.
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial