Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update of parent-child tables on SQl server does not work!

Status
Not open for further replies.

likhtin

Programmer
Mar 31, 2006
8
0
0
Hi all
I have form and three DataGridView on it. They are binded to DataTable's which has relation as Parent-Children-Grandchildren, tables on MSQ SQl server has same relations.
To save changes which user makes in that objects I use method Update() of respective tableadapters. I call methods in respective sequence Parent,Children, Grandchildren
If user add record to Parent and Children and then saves changes everything works fine. But if he added records into all three objects and saves changes he gets error message that record in Grandchildren violate foreign key to Children table. Message generated by MS SQL server.
I can not understand reason for it. On a moment of updating of Grandchildren table, respective records already commited to Children table..
Any thoughts?
Info toward:
Adapters do update through store procedures., all tables involved have identity fields.
 
Make sure that the child table is getting populated and commited before the insert statement starts firing on grandchild table. This is one of the common possibility.

Prashant
 
I even do more
I thought that off line copy of date should be awared that previous insrets went trhough so retrieve to dataset latest changes from database. But it still does not work.
Below code of whole method
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Validate();
int investorPos = spInvestorBindingSource.Position;
int accountPos = spInvestorspAccountBindingSource.Position;
int balancePos = spAccountspBalanceBindingSource.Position;

try
{

// this is Parent
this.spInvestorTableAdapter.Update(this.dsInvestorAccountBalance.spInvestor);
this.spInvestorTableAdapter.Fill(this.dsInvestorAccountBalance.spInvestor);

// this is Child
this.spAccountTableAdapter.Update(this.dsInvestorAccountBalance.spAccount);
this.spAccountTableAdapter.Fill(this.dsInvestorAccountBalance.spAccount);


//this is Grandchild
this.dsInvestorAccountBalance.spBalance.AcceptChanges();


AppStart.setarithabort(this.spBalanceTableAdapter.Connection);

this.spBalanceTableAdapter.Update(this.dsInvestorAccountBalance.spBalance);
this.spBalanceTableAdapter.Fill(this.dsInvestorAccountBalance.spBalance);
MessageBox.Show("Changes in whole form are saved");
refreshToolStripMenuItem_Click(sender, e);
}
catch (Exception ex)
{
MessageBox.Show("Error happeend during saving data. Read message and let know programmer\n" + ex.Message);
}
finally
{
spInvestorBindingSource.Position = investorPos;
spInvestorspAccountBindingSource.Position = accountPos;
spAccountspBalanceBindingSource.Position = balancePos;
}


}
 
Have you looked on how you have defined the data relations between "Children" and "GrandChildren"? I haven't actually used .net 2.0 data relation objects, but I believe you can set some properties to define a cascaded update/delete on foreign key constraints. So that, when the "Children"'s records referenced PK is updated, it will cascade that value to its corresponding "GrandChildren" records.

Just my 2 cents. ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top