Updating Related Tables
-
I have three tables, a main table and two subtables that are related to the main table by the main tables key field. In my application I set up three OleDbDataAdapters to read three seperate tables into three tables of a dataset and then I add to the datasets relation collection to link the tables together. The problem is that while I can update the main table by calling the update method of it's DataAdapter, I can't do the same with the other two tables. When I call their update method, nothing happens. I get no errors and the rows that where marked to be updated are still marked to be updated (i.e. AcceptChanges hasn't been called). Also, I can't change the key field even in the main table. Changes to the key simply get ignored and don't get written back to the database. Does anybody know what is happening here? I suspect it's something to do with the tables being related.
-
I have three tables, a main table and two subtables that are related to the main table by the main tables key field. In my application I set up three OleDbDataAdapters to read three seperate tables into three tables of a dataset and then I add to the datasets relation collection to link the tables together. The problem is that while I can update the main table by calling the update method of it's DataAdapter, I can't do the same with the other two tables. When I call their update method, nothing happens. I get no errors and the rows that where marked to be updated are still marked to be updated (i.e. AcceptChanges hasn't been called). Also, I can't change the key field even in the main table. Changes to the key simply get ignored and don't get written back to the database. Does anybody know what is happening here? I suspect it's something to do with the tables being related.
-
Okay, thanks for your reply. Here's an example of the problem I'm having. I've included some of the code I've added just to try and figure out what's happening. When I iterate throw the rows of the child table I find that all the rows have RowState = Unchanged, even though some of them have an Original and a Proposed value. I believe they are not getting updated because the RowState is Unchanged, but I can't figure out why the RowState is Unchanged when there is clearly a Proposed value available. Surely setting a Proposed value for a row should automatically change the RowState?
public void UpdateData() { connDB.Open(); // I tried to intecept the RowUpdating events, but they never actually occur! SubApt.RowUpdating += new OleDbRowUpdatingEventHandler(MyRowUpdateHandler); // Try to find out what the status of the rows in the child table actually is. string s = ""; foreach (DataRow thisRow in m_myDS.Tables["tblSub"].Rows) { s += thisRow["Key"] + " -> " + thisRow["Field1"] + " " + thisRow.RowState.ToString() + "\n"; // RowState is ALWAYS Unchanged even though some rows have a proposed value! if (thisRow.HasVersion(DataRowVersion.Proposed)) { // The proposed value really is the value it's supposed to be changed to, and the original value // really is the original value s += thisRow["Field1", DataRowVersion.Original] + " " + thisRow["Field1", DataRowVersion.Proposed] + "\n"; } } MessageBox.Show(s); // the main table updates just fine MainApt.Update(m_myDS.Tables["tblMain"]); // but this does nothing!!! It doesn't matter which way around I update the two tables, or even if I leave // out the main table altogether SubApt.Update(m_myDS.Tables["tblSub"]); connDB.Close(); }
-
Okay, thanks for your reply. Here's an example of the problem I'm having. I've included some of the code I've added just to try and figure out what's happening. When I iterate throw the rows of the child table I find that all the rows have RowState = Unchanged, even though some of them have an Original and a Proposed value. I believe they are not getting updated because the RowState is Unchanged, but I can't figure out why the RowState is Unchanged when there is clearly a Proposed value available. Surely setting a Proposed value for a row should automatically change the RowState?
public void UpdateData() { connDB.Open(); // I tried to intecept the RowUpdating events, but they never actually occur! SubApt.RowUpdating += new OleDbRowUpdatingEventHandler(MyRowUpdateHandler); // Try to find out what the status of the rows in the child table actually is. string s = ""; foreach (DataRow thisRow in m_myDS.Tables["tblSub"].Rows) { s += thisRow["Key"] + " -> " + thisRow["Field1"] + " " + thisRow.RowState.ToString() + "\n"; // RowState is ALWAYS Unchanged even though some rows have a proposed value! if (thisRow.HasVersion(DataRowVersion.Proposed)) { // The proposed value really is the value it's supposed to be changed to, and the original value // really is the original value s += thisRow["Field1", DataRowVersion.Original] + " " + thisRow["Field1", DataRowVersion.Proposed] + "\n"; } } MessageBox.Show(s); // the main table updates just fine MainApt.Update(m_myDS.Tables["tblMain"]); // but this does nothing!!! It doesn't matter which way around I update the two tables, or even if I leave // out the main table altogether SubApt.Update(m_myDS.Tables["tblSub"]); connDB.Close(); }
1. Did you call AcceptChanges somewhere? Don't call AcceptChanges before Adap.Update(). 2. Clear the EventHandler for RowUpdate. This may call AcceptChanges() 3. Try to edit a Row and immediatly afterwards check it's RowState. If the RowState is unchanged I don't know what's wrong. AcceptChanges is a tricky thing: after this call all Rows are marked unchanged! So there is nothing what could be Updated by the Update Method of Apt. Please inform me about Point 3. Stefan
-
1. Did you call AcceptChanges somewhere? Don't call AcceptChanges before Adap.Update(). 2. Clear the EventHandler for RowUpdate. This may call AcceptChanges() 3. Try to edit a Row and immediatly afterwards check it's RowState. If the RowState is unchanged I don't know what's wrong. AcceptChanges is a tricky thing: after this call all Rows are marked unchanged! So there is nothing what could be Updated by the Update Method of Apt. Please inform me about Point 3. Stefan
STW wrote: 1. Did you call AcceptChanges somewhere? Don't call AcceptChanges before Adap.Update(). No, I have not called AcceptChanges explictly anywhere in my program. I had thought that perhaps calling update on the main table might automatically call AcceptChanges on the child table, but not updating the main table didn't fix the problem. I was also thinking that calling AcceptChanges should move the proposed value into the original value shouldn't it? That's not been happening. STW wrote: 2. Clear the EventHandler for RowUpdate. This may call AcceptChanges() I tried it without the EventHandler and it makes no difference. I only put the EventHandler there in the hopes of being able to intercept the call and figure out what was happening. As it turns out, the event never gets fired. STW wrote: 3. Try to edit a Row and immediatly afterwards check it's RowState. If the RowState is unchanged I don't know what's wrong. AcceptChanges is a tricky thing: after this call all Rows are marked unchanged! So there is nothing what could be Updated by the Update Method of Apt. Okay, I tried manually changing the value of one field in the child table like this:
m_myDS.Tables["subtable"].Rows[0]["Field1"] = 101;
And if I then iterate through the rows in the table, I can see that row has been changed to RowState.Modified and it doesn't have a proposed value anymore. Further, when I call the update that row does, in fact, get updated in the database. -
STW wrote: 1. Did you call AcceptChanges somewhere? Don't call AcceptChanges before Adap.Update(). No, I have not called AcceptChanges explictly anywhere in my program. I had thought that perhaps calling update on the main table might automatically call AcceptChanges on the child table, but not updating the main table didn't fix the problem. I was also thinking that calling AcceptChanges should move the proposed value into the original value shouldn't it? That's not been happening. STW wrote: 2. Clear the EventHandler for RowUpdate. This may call AcceptChanges() I tried it without the EventHandler and it makes no difference. I only put the EventHandler there in the hopes of being able to intercept the call and figure out what was happening. As it turns out, the event never gets fired. STW wrote: 3. Try to edit a Row and immediatly afterwards check it's RowState. If the RowState is unchanged I don't know what's wrong. AcceptChanges is a tricky thing: after this call all Rows are marked unchanged! So there is nothing what could be Updated by the Update Method of Apt. Okay, I tried manually changing the value of one field in the child table like this:
m_myDS.Tables["subtable"].Rows[0]["Field1"] = 101;
And if I then iterate through the rows in the table, I can see that row has been changed to RowState.Modified and it doesn't have a proposed value anymore. Further, when I call the update that row does, in fact, get updated in the database.Okay, I just figured something out. If, while I'm iterating through the rows, I call EndEdit on each row it will set the RowState to modified (if it has actually been modified). So the problem seem to be that when a datarow is bound to my form, BeginEdit is implicitly called, and when that particular row becomes unbound, it should automatically call EndEdit on that row. But it isn't doing it for my child table. Now, I don't want to have to iterate through all my records and call EndEdit on every one of them before I run my update because I have a lot of records and I expect it to grow rapidly once (if) I finish my app. I'm thinking I could intercept the PositionChanged event of my CurrencyManager, but I think at that point the position has already changed so I won't be able to grab the previous row to call EndEdit on it. I could try and intercept every button or event that results in a change of position and call EndEdit before I change the position, but that's a pain since I'd have to remember to do that in several different places. I'd also have to make sure I call EndEdit on the current row when the Update button is clicked, but that's not a major problem (because it's only one place). So I think I can work around my problem, but I'd be much happier if I could just figure out why the CurrencyManager isn't calling EndEdit and fix it!