How to transfer these data row changes back to the database

I have not worked with datasets before. I use a lot of LINQ / Entity Framework.

Here is the code I wrote (this is part of the switch):

if (!DataHelper.DataSourceIsEmpty(dsUp)) { //get datarow collection from dataset DataRowCollection drc = dsUp.Tables[0].Rows; //Loop through dataset foreach (DataRow dr in drc) { //get current dataset row sortid int sortID = Convert.ToInt32(dr["SortID"]); { //if its the row above then minus one if (sortID == nodeAbove) { int newID = Convert.ToInt32(dr["SortID"].ToString()); newID--; dr["SortID"] = newID; //TODO: save changes back to original ds } } } } break; 

I tried things like:

  • dr.AcceptChanges
  • dsUp.AcceptChanges (dr)
  • drc.copyto (dsUP)
  • dsUp.Merge (DRC)

and many other similar attempts that did not work. When googling for this topic all the results that I found use a table adapter ... since im works in cms, I get my data as follows:

 DataSet dsUp = tree.SelectNodes(CurrentSite, path, cultureCode, true, classnames, where, orderby); 

Any help on making changes saved back to db would be greatly appreciated by Cheers.

After posting, I also tried this method, which unfortunately did not work:

  //dataset to hold results before merge DataSet DSResults = tree.SelectNodes(CMSContext.CurrentSite.SiteName, path, cultureCode, true, classnames); DSResults.Clear(); if (!DataHelper.DataSourceIsEmpty(dsUp)) { //get datarow collection from dataset DataRowCollection drc = dsUp.Tables[0].Rows; //Loop through dataset foreach (DataRow dr in drc) { //get current dataset row sortid int sortID = Convert.ToInt32(dr["SortID"]); { //if its the row above then minus one if (sortID == nodeAbove) { int newID = Convert.ToInt32(dr["SortID"].ToString()); newID--; dr["SortID"] = newID; dr.AcceptChanges(); DSResults.Tables[0].Rows.Add(dr); } } } } //save changes back to original ds dsUp.Merge(DSResults); dsUp.AcceptChanges(); break; 
+4
source share
1 answer

The data set behind the scene implements the UnitOfWork template, which tracks all the changes you made after you pulled the data from the database.

what you skip here causes a dataset update to save all changes to the DB

I have added an update to your code:

 if (!DataHelper.DataSourceIsEmpty(dsUp)) { //get datarow collection from dataset DataRowCollection drc = dsUp.Tables[0].Rows; //Loop through dataset foreach (DataRow dr in drc) { //get current dataset row sortid int sortID = Convert.ToInt32(dr["SortID"]); { //if its the row above then minus one if (sortID == nodeAbove) { int newID = Convert.ToInt32(dr["SortID"].ToString()); newID--; dr["SortID"] = newID; //TODO: save changes back to original ds } } } //you can save here as the dataset will keep track of all the changes YourDataAdapter.Update("tableName",dsUp) } 
+2
source

All Articles