Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Updating Related Tables

Updating Related Tables

Scheduled Pinned Locked Moved Database
questiondatabasehelpannouncement
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wjousts
    wrote on last edited by
    #1

    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.

    S 1 Reply Last reply
    0
    • W Wjousts

      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.

      S Offline
      S Offline
      STW
      wrote on last edited by
      #2

      Please show your code which updates the Child Tables. Stefan

      W 1 Reply Last reply
      0
      • S STW

        Please show your code which updates the Child Tables. Stefan

        W Offline
        W Offline
        Wjousts
        wrote on last edited by
        #3

        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(); }

        S 1 Reply Last reply
        0
        • W Wjousts

          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(); }

          S Offline
          S Offline
          STW
          wrote on last edited by
          #4

          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

          W 1 Reply Last reply
          0
          • S STW

            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

            W Offline
            W Offline
            Wjousts
            wrote on last edited by
            #5

            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.

            W 1 Reply Last reply
            0
            • W Wjousts

              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.

              W Offline
              W Offline
              Wjousts
              wrote on last edited by
              #6

              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!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups