Try OpenEdge Now
skip to main content
.NET Open Clients
Passing Parameters : Passing DATASET and DATASET-HANDLE parameters : ProDataSet examples : Sample ProDataSet update
 
Sample ProDataSet update
The code examples in this section show how to pass a changes-only DataSet (that is, a DataSet that contains only records that have been added, updated, or deleted) to the AppServer and properly process the changes.
The following example shows one way to handle updates in a .NET Open Client environment.
.NET update DataSet procedure
//Define two strongly-typed DataSets
using Acme.StrongTypesNS;
dsOrderDataSet dsOrders;
dsOrderDataSet dsUpdatedRecs;
      .
      .
      .
// Populate the dsUpdatedRecs DataSet with only the changed rows
dsUpdatedRecs = (dsOrderDataSet)
(dsOrders.GetChanges( ));
// Confirm that there are updates before continuing
if (dsUpdatedRecs != null)
   {
      appObj.UpdateDS(ref dsUpdatedRecs);
      if (dsUpdateRecs.Tables["orderline"].HasErrors)
         {
            displayMessage = "The following Orderline rows were not updated:";
            foreach(DataRow failedRow in
                    dsUpdatedRecs.Tables ["orderline"].Rows)
            {
               if (failedRow.HasErrors)
               {
                 displayMessage += "\n\r Line number: " +
                       failedRow["linenum"].ToString( ) + ",
                 Error Message: " + failedRow.RowError;
               }
                .
                .
                .
            }
        }
     }
else
   {
      dspMsg ="All updates succeeded";
      dspCaption = "Save completed";
      dspButton = MessageBoxButtons.OK;
      resultDlog = MessageBox.Show(this, dspMsg, dspCaption, dspButton,          MessageBoxIcon.Information,
         MessageBoxDefaultButton.Button1,
         MessageBoxOptions.RightAlign);
   }
// Remove the pending additional order lines before merging to prevent
// duplicate rows if the key fields on the row number were changed during the // update.
foreach(DataRow curRow in dtTableTwo.Select("", "", DataViewRowState.Added))
   {
      dtTableTwo.Rows.Remove(curRow);
   }
// Merge the changes
dsOrders.Merge(dsUpdatedRecs);

// Reset the row state for the modified rows now that changes
dsOrders.AcceptChanges( );
Note: For the sake of simplicity, this example does not include standard error checking. Make sure that you include error checking in your application code.
The following example shows temp-table definitions in an ABL include file, sOrderTables.i.
Include file definition of temp-table definitions
/* dsOrderTables.i -- include file for Temp-Table definitions */
DEFINE TEMP-TABLE ttOrder FIELDS (...)
  INDEX OrderNum IS UNIQUE PRIMARY OrderNum.
DEFINE TEMP-TABLE ttOLine FIELDS (...) BEFORE-TABLE ttOlineBefore
  INDEX orderline IS UNIQUE PRIMARY Ordernum Linenum.
The following example shows a static ProDataSet definition in an ABL include file, dsOrderDef.i.
Include file definition of DataSet dsOrder
/* dsOrderDef.i -- include file definition of DATASET dsOrder. */
DEFINE DATASET dsOrder FOR ttOrder, ttOLine
  DATA-RELATION OrderLine FOR ttOrder, ttOLine
    RELATION-FIELDS (OrderNum, OrderNum).
The following example shows an ABL procedure that updates the database with a DataSet that contains only records that have been modified (added, updated, or deleted).
ABL update DataSet procedure
{OrderTables.i}
{OrderDS.i}

DEFINE INPUT-OUTPUT PARAMETER DATASET FOR dsOrder.

DEFINE VARIABLE hSrcOline  AS HANDLE  NO-UNDO.
DEFINE VARIABLE returnValue AS LOGICAL NO-UNDO.

DEFINE DATA-SOURCE srcOline FOR Orderline.
hSrcOline = DATA-SOURCE srcOline:HANDLE.

BUFFER ttOLine:ATTACH-DATA-SOURCE(hSrcOline).

FOR EACH ttOlineBefore TRANSACTION:
  returnValue = BUFFER ttOlineBefore:SAVE-ROW-CHANGES("Orderline") NO-ERROR.
END.

DELETE OBJECT hSrcOline.
RETURN.