A
Alex
We're trying to minimize the number of postbacks to the server in our web
application by allowing users to change rows in a grid on the client side.
When they click the submit button, we need to update the changes to the
database.
I have the original DataSet and I have the updated one which is built off
the current grid. I now need to compare and find out what was updated,
deleted and inserted before proceeding with the database updates.
Both have the same schema and a uniqie index. Other than looping for each
row and comparing the two, is there an easier and simpler way to get the
changes? I tried merge but it didn't pick up the changes at all.
Here is the code I'm using. All the records are unchanged so dsDifference is
always empty.
// Get from session.
DataSet dsOriginal = (DataSet)Session["ds"];
// Create a DS using grid data.
DataSet dsChanged = base.GridToDataSet(UltraWebGrid1);
// Add primary keys.
dsOriginal.Tables[0].PrimaryKey = new DataColumn[] {
dsOriginal.Tables[0].Columns["ObjId"] };
dsChanged.Tables[0].PrimaryKey = new DataColumn[] {
dsChanged.Tables[0].Columns["ObjId"] };
// Merge.
dsOriginal.Merge(dsChanged);
// Create the delta DS.
DataSet dsDifference = new DataSet();
dsDifference = dsOriginal.Clone();
foreach(DataRow row in dsOriginal.Tables[0].Rows)
{
if(row.RowState == DataRowState.Modified)
dsDifference.Tables[0].Rows.Add(row);
}
Thanks
Alex
application by allowing users to change rows in a grid on the client side.
When they click the submit button, we need to update the changes to the
database.
I have the original DataSet and I have the updated one which is built off
the current grid. I now need to compare and find out what was updated,
deleted and inserted before proceeding with the database updates.
Both have the same schema and a uniqie index. Other than looping for each
row and comparing the two, is there an easier and simpler way to get the
changes? I tried merge but it didn't pick up the changes at all.
Here is the code I'm using. All the records are unchanged so dsDifference is
always empty.
// Get from session.
DataSet dsOriginal = (DataSet)Session["ds"];
// Create a DS using grid data.
DataSet dsChanged = base.GridToDataSet(UltraWebGrid1);
// Add primary keys.
dsOriginal.Tables[0].PrimaryKey = new DataColumn[] {
dsOriginal.Tables[0].Columns["ObjId"] };
dsChanged.Tables[0].PrimaryKey = new DataColumn[] {
dsChanged.Tables[0].Columns["ObjId"] };
// Merge.
dsOriginal.Merge(dsChanged);
// Create the delta DS.
DataSet dsDifference = new DataSet();
dsDifference = dsOriginal.Clone();
foreach(DataRow row in dsOriginal.Tables[0].Rows)
{
if(row.RowState == DataRowState.Modified)
dsDifference.Tables[0].Rows.Add(row);
}
Thanks
Alex