I have not really worked with datasets before. I use a lot of LINQ / Entity Framework.
Here is the code I have written (it is one part of a 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 have been trying things like:
- dr.AcceptChanges
- dsUp.AcceptChanges(dr)
- drc.copyto(dsUP)
- dsUp.Merge(drc)
and many other similar attempts that have not worked. When googling for this topic all the results I have found use a table adapter... as im working in a cms I get my data like so:
DataSet dsUp = tree.SelectNodes(CurrentSite, path, cultureCode, true, classnames, where, orderby);
Any assistance on getting the changes saved back to the db would be massively appreciated Cheers.
Since posting I have 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;