asp.net.ph

DataSet.GetChanges Method ( )

System.Data Namespace   DataSet Class


Returns a copy of the DataSet that contains all changes made to it since it was loaded or AcceptChanges was last called.

[ VB ]
Overloads Public Function GetChanges ( ) As DataSet

[ C# ]
public DataSet GetChanges ( );

[ C++ ]
public: DataSet* GetChanges ( );

[ JScript ]
public function GetChanges ( ) : DataSet;

Return Value

A copy of the changes from this DataSet that can have actions performed on it and subsequently be merged back in using Merge, or a null reference if none are found.

Remarks

Returns a copy of the DataSet that contains all changes made to it since it was loaded or AcceptChanges was last called. This copy is particularly designed so that it can be merged back in to this original DataSet. Relationship constraints may cause Unchanged parent rows to be included. If no rows of these rowStates are found, this method returns a null reference.

Example

The following example initializes a simple DataSet with one table, two columns, and ten rows. Two values are changed, and one row is added. A subset of the changed data is created using the GetChanges method. After reconciling errors, a new column is added to the subset, changing the schema. When the Merge method is called with the missingSchemaAction set to MissingSchemaAction.Add. the new column is added to the original DataSet object's schema.

private void DemonstrateMerge ( ) {
   // create a DataSet with one table, two columns, and three rows.
   DataSet ds = new DataSet ( "myDataSet" );
   DataTable tbl = new DataTable ( "Items" );
   DataColumn c1 = new DataColumn ( "id",
      Type.GetType ( "System.Int32" ) );
   c1.AutoIncrement = true;
   DataColumn c2 = new DataColumn ( "Item",
      Type.GetType ( "System.Int32" ) );
   // dataColumn array to set primary key.
   DataColumn [ ] keyCol = new DataColumn [ 1 ];
   DataRow row;
   // create variable for temporary DataSet.
   DataSet xSet;
   // add columns to table, and table to DataSet.
   tbl.Columns.Add ( c1 );
   tbl.Columns.Add ( c2 );
   ds.Tables.Add ( tbl );
   // set primary key column.
   keyCol [ 0 ] = c1;
   tbl.PrimaryKey = keyCol;
   // add ten rows.
   for ( int i = 0; i <10; i++ ) {
      row = tbl.NewRow ( );
      row [ "Item" ] = i;
      tbl.Rows.Add ( row );
  }
   // accept changes.
   ds.AcceptChanges ( );
   PrintValues ( ds, "Original values" );
   // change two row values.
   tbl.Rows [ 0 ] [ "Item" ] = 50;
   tbl.Rows [ 1 ] [ "Item" ] = 111;
   // add one row.
   row = tbl.NewRow ( );
   row [ "Item" ] = 74;
   tbl.Rows.Add ( row );
   // insert code for error checking. Here we set one row in error.
   tbl.Rows [ 1 ].RowError = "over 100";
   PrintValues ( ds, "Modified and New Values" );
   // if the table has changes or errors, create a subset DataSet.
   if ( ds.HasChanges ( DataRowState.Modified | DataRowState.Added )
         & ds.HasErrors ) {
      // use GetChanges to extract subset.
      xSet = ds.GetChanges ( DataRowState.Modified|DataRowState.Added );
      PrintValues ( xSet, "Subset values" );
      // insert code to reconcile errors. In this case, we'll reject changes.

      foreach ( DataTable xTable in xSet.Tables ) {
         if ( xTable.HasErrors ) {
            foreach ( DataRow xRow in xTable.Rows ) {
               // Response.Write ( xRow [ "Item" ] );
               if ( ( int ) xRow [ "Item",DataRowVersion.Current ] > 100 ) {
                  xRow.RejectChanges ( );
                  xRow.ClearErrors ( );
              }
           }
        }
     }
      PrintValues ( xSet, "Reconciled subset values" );
      // merge changes back to first DataSet.
      ds.Merge ( xSet );
      PrintValues ( ds, "Merged Values" );
   }
}

private void PrintValues ( DataSet ds, string label ) {
   Response.Write ( "<br>" + label );

   foreach ( DataTable tbl in ds.Tables ) {
      Response.Write ( "TableName: " + tbl.TableName );

      foreach ( DataRow row in tbl.Rows ) {

         foreach ( DataColumn col in tbl.Columns ) {
            Response.Write ( ( "\t " + row [ col ] );
         }
         Response.Write ( );
      }
    }
}
  C# VB

See Also

DataSet Members   DataSet.GetChanges Overload List   HasChanges   HasErrors Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph