How to save a relations child table?

thomas10001

Well-known member
Joined
Jun 22, 2003
Messages
57
Location
Samoa & Sweden
I have two dataadapters

oleDbDataAdapterTournament and
oleDbDataAdapterGroup

// they have the wizard generated sql for table tournament respectively group. I have also a dataadapter

dsTournament1

which has the both tables (tournament and group). I also have connected a relation in the dataset (called tournamentgroup) to connect tournament.groupid to group.groupid

I have a listbox which shows the tournament.name and a textbox
which shows the correlated group for the tournament.
(connected to .text property as dsTournament1 - tournament.tournamentgroup.groupname)
Everything works fine in changing the master and the child shows the corresponding value.

But when I want to save .Update the data for the group table I doesnt save.

I have tried this code for saving

this.BindingContext[dsTournament1,"group"].EndCurrentEdit();
oleDbDataAdapterGroup.Update(dsTournament1,"group");
dsTournament1.AcceptChanges();

I dont know if its possible to save the child table but I assume it is. Is this that the correct way to address the group table to make it save or should the the relation object be used somehow in the bindingcontext and update method to make it work?



FYI the load event looks like this:

dsTournament1.Clear();
oleDbDataAdapterTournament.Fill(dsTournament1);
oleDbDataAdapterGroup.Fill(dsTournament1);

and save code (which works fine) for tournament table looks like this:

this.BindingContext [dsTournament1,"tournament"].EndCurrentEdit();
oleDbDataAdapterTournament.Update(dsTournament1);
dsTournament1.AcceptChanges();


Thanks!
 
Try swapping AcceptChanges and Update:

this.BindingContext[dsTournament1,"group"].EndCurrentEdit();
dsTournament1.AcceptChanges();
oleDbDataAdapterGroup.Update(dsTournament1,"group");
 
Hmmm... I think AcceptChanges() updates the local data copies only and not the underlying data source while DataAdapter.Update affects the data source. Thats why you have to call AcceptChanges() first before Update().

Anyway, are you sure the UpdateCommand of the DataAdapter has been properly set? What is the value of DataAdapter.UpdateCommand.CommandText?
 
Make sure that when you fill the dataset that you reference the table names, otherwise it will think those two DataAdapters are the same table:
oleDbDataAdapterTournament.Fill(dsTournament1);
oleDbDataAdapterGroup.Fill(dsTournament1);

Should probably look like:
oleDbDataAdapterTournament.Fill(dsTournament1, "Tournament");
oleDbDataAdapterGroup.Fill(dsTournament1, "Group");


/cc

(edit: typos)
 
Originally posted by JABE
Try swapping AcceptChanges and Update:

this.BindingContext[dsTournament1,"group"].EndCurrentEdit();
dsTournament1.AcceptChanges();
oleDbDataAdapterGroup.Update(dsTournament1,"group");


Originally posted by JABE
Hmmm... I think AcceptChanges() updates the local data copies only and not the underlying data source while DataAdapter.Update affects the data source. Thats why you have to call AcceptChanges() first before Update().

Please be careful with uncertain recommendations. It makes future use of the post difficult and confuses those less experienced with .Net.

When you call an acceptchanges, the rowstate is reset to current and original. Consequently, if you call an acceptchanges before you call an update on your dataadapter, it wont read a modification in the data and no insert, delete, or update commands will be implemented against the db. The .fill and .update methods automatically call an acceptchanges as part of their implementation. An acceptchanges call really only needs to be made explicitly after an .executenonquery to manually update the status of the rowstate in a dataset.


Jon
 
Last edited by a moderator:
Yup, jfackler is correct. Call the Update first before AcceptChanges, silly me. Sorry, thomas10001 and thanks, jfackler.
 
// the fill is working fine and are after an example from the walkthroughs.
oleDbDataAdapterTournament.Fill(dsTournament1);
oleDbDataAdapterGroup.Fill(dsTournament1);



This is what I have already done. But it is not working

this.BindingContext[dsTournament1,"group"].EndCurrentEdit();
oleDbDataAdapterGroup.Update(dsTournament1,"group");
dsTournament1.AcceptChanges(); // which is not neccessary

I suspect there is something to do with the fact it is a relation. Is there another way to refer to the group table? ie with the relation name?
 
It is working now!

this was the code for saving

this.BindingContext[dsTournament1,"group"].EndCurrentEdit();
oleDbDataAdapterGroup.Update(dsTournament1,"group");

I just found out that this code is correct. It was apparently a fault in the code where I add rows to the group table in dsTournament1. Thank you all anyway for your help!
 
It turns out that I have do set the value programatically (in the dataset) in the text_changed event for the textbox connected to the child table.
Then it will be saved when I run Update.

If I change value for two columns (two textboxes for the child table) only one will be saved when Update is done.

Should I do Update after each programatically set of the child or maybe call endedit at the end of the text_changed event?

Isnt there a better solution to all this? I expected it would all be done automatically by some manager!
 
Back
Top