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. General Programming
  3. C#
  4. GetChanges() & AcceptChanges() not working....

GetChanges() & AcceptChanges() not working....

Scheduled Pinned Locked Moved C#
question
8 Posts 3 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.
  • S Offline
    S Offline
    sjs4u
    wrote on last edited by
    #1

    Hi, I want to get updated fields row from datatable or dataset. I used Getchanges() on dataset but didnt work at all. How can I Get the updated rows only from existing dataset or datatable. Thanks sjs

    S 1 Reply Last reply
    0
    • S sjs4u

      Hi, I want to get updated fields row from datatable or dataset. I used Getchanges() on dataset but didnt work at all. How can I Get the updated rows only from existing dataset or datatable. Thanks sjs

      S Offline
      S Offline
      Som Shekhar
      wrote on last edited by
      #2

      You need to provide some code to know what is happening there. We all use these functions and they work for us. So you must be doing something wrong. right?

      S 1 Reply Last reply
      0
      • S Som Shekhar

        You need to provide some code to know what is happening there. We all use these functions and they work for us. So you must be doing something wrong. right?

        S Offline
        S Offline
        sjs4u
        wrote on last edited by
        #3

        hi, let me clarify now. string mySelectQuery = "SELECT * from Table1"; SqlConnection myConnection = new SqlConnection(connectionString); SqlDataAdapter adpt = new SqlDataAdapter(mySelectQuery, myConnection); dt = new DataTable(); myConnection.Open(); adpt.Fill(ds); dt = ds.Tables[0]; dataGridView1.DataSource = dt; myConnection.Close(); This is written on Refresh button. Now there is another button where i written code to get only updated row from above datatable. DataSet ds3 = new DataSet(); dt.AcceptChanges(); // dt delcared globally ds3 = dt.GetChanges(); If I change one of the field in table1 directly from database and if i clicked on refresh button then it is showing the updated field. Now if i clicked on second button then I want only that updated row and want to store in dataset ds3. Just let me know if i m doing any wrong from above code. Thanks sjs

        S D 2 Replies Last reply
        0
        • S sjs4u

          hi, let me clarify now. string mySelectQuery = "SELECT * from Table1"; SqlConnection myConnection = new SqlConnection(connectionString); SqlDataAdapter adpt = new SqlDataAdapter(mySelectQuery, myConnection); dt = new DataTable(); myConnection.Open(); adpt.Fill(ds); dt = ds.Tables[0]; dataGridView1.DataSource = dt; myConnection.Close(); This is written on Refresh button. Now there is another button where i written code to get only updated row from above datatable. DataSet ds3 = new DataSet(); dt.AcceptChanges(); // dt delcared globally ds3 = dt.GetChanges(); If I change one of the field in table1 directly from database and if i clicked on refresh button then it is showing the updated field. Now if i clicked on second button then I want only that updated row and want to store in dataset ds3. Just let me know if i m doing any wrong from above code. Thanks sjs

          S Offline
          S Offline
          Som Shekhar
          wrote on last edited by
          #4

          sjs4u wrote:

          DataSet ds3 = new DataSet(); dt.AcceptChanges(); // dt delcared globally ds3 = dt.GetChanges();

          After you say AcceptChanges, there are no changes left. Hence you will get null in GetChanges method. I suggest adding an AcceptChanges when you load your table. This way any changes maade during the program will be shown when you want to.

          S 1 Reply Last reply
          0
          • S Som Shekhar

            sjs4u wrote:

            DataSet ds3 = new DataSet(); dt.AcceptChanges(); // dt delcared globally ds3 = dt.GetChanges();

            After you say AcceptChanges, there are no changes left. Hence you will get null in GetChanges method. I suggest adding an AcceptChanges when you load your table. This way any changes maade during the program will be shown when you want to.

            S Offline
            S Offline
            sjs4u
            wrote on last edited by
            #5

            I write it after adpt.Fill(ds) It is showing in the dt that value is updated but when i click on 2nd button still it is not showing that perticular updated row.. Can you show it with the above example

            S 1 Reply Last reply
            0
            • S sjs4u

              I write it after adpt.Fill(ds) It is showing in the dt that value is updated but when i click on 2nd button still it is not showing that perticular updated row.. Can you show it with the above example

              S Offline
              S Offline
              Som Shekhar
              wrote on last edited by
              #6

              see, its simple.

              DataSet ds = new DataSet("DateSet");
              ds.Tables.Add(new DataTable());

              LoadTable(ds.Tables[0]); //Add your dataadapter work here
              ds.AcceptChanges(); //If Not done during loading. Otherwise remove.

              ds.Tables[0].Rows.Add(50,20,10); //Replace with your code to edit a row.

              DataTable changedTable = ds.Tables[0].GetChanges(); //Here you should get one row.

              S 1 Reply Last reply
              0
              • S Som Shekhar

                see, its simple.

                DataSet ds = new DataSet("DateSet");
                ds.Tables.Add(new DataTable());

                LoadTable(ds.Tables[0]); //Add your dataadapter work here
                ds.AcceptChanges(); //If Not done during loading. Otherwise remove.

                ds.Tables[0].Rows.Add(50,20,10); //Replace with your code to edit a row.

                DataTable changedTable = ds.Tables[0].GetChanges(); //Here you should get one row.

                S Offline
                S Offline
                sjs4u
                wrote on last edited by
                #7

                ok thanks... I will try it

                1 Reply Last reply
                0
                • S sjs4u

                  hi, let me clarify now. string mySelectQuery = "SELECT * from Table1"; SqlConnection myConnection = new SqlConnection(connectionString); SqlDataAdapter adpt = new SqlDataAdapter(mySelectQuery, myConnection); dt = new DataTable(); myConnection.Open(); adpt.Fill(ds); dt = ds.Tables[0]; dataGridView1.DataSource = dt; myConnection.Close(); This is written on Refresh button. Now there is another button where i written code to get only updated row from above datatable. DataSet ds3 = new DataSet(); dt.AcceptChanges(); // dt delcared globally ds3 = dt.GetChanges(); If I change one of the field in table1 directly from database and if i clicked on refresh button then it is showing the updated field. Now if i clicked on second button then I want only that updated row and want to store in dataset ds3. Just let me know if i m doing any wrong from above code. Thanks sjs

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  Read the documentation on GetChanges and AcceptChanges. Those functions do things different than what your code suggests they do. GetChanges returns a DataSet/DataTable object with the changes in the DataSet/DataTable. It does NOT get the last set of changes from the database. DataSet/DataTable objects keep track of which records they hold are "dirty" an need to be written back to the database. AcceptChanges tells the DataSet/DataTable object to reset all the "dirty", "new", and "deleted" flags to "original" (can't remember the actual enum names right now!) so the DataSet/DataTable thinks there are no changes to be written back to the database.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007, 2008
                  But no longer in 2009...

                  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