GetChanges() & AcceptChanges() not working....
-
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
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?
-
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?
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
-
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
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.
-
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.
-
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
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.
-
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.
-
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
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...