TransactionsAndDataSets 
==================

The TransactionsAndDataSets application illustrates various basic behaviors of a Transaction, and also the use of the DataSet.RejectChanges() method.  The latter operates on pending changes to a DataSet rather than pending changes to the back-end database, but otherwise functions in a manner somewhat similar to the Transaction.Rollback() method.

Try the following experiments. After observing the results of each experiment, you may want to re-perform it with a break point set at the first executable statement in the form's AddRecord() method so you can see the exact operations being performed.

Experiment No. 1

This experiment demonstrates the use of the DataSets RejectChanges() method. Records are loaded from the Northwind Products table; a new record is added and displays in a DataGrid; and upon invoking RejectChanges(), the newly added record disappears.

Perform the experiment using the following steps:

1. Launch the program

2. Click the <Load Records from Back-End Database> button to load records from the Northwind Products table.  The records will be sorted in descending order by ProductId.

3. Make sure the  "Update the back-end database" checkbox is set to False.

4. Click the <Add Record> button.

A new product record is added to the Products table of the objdsNw DataSet.  The table is also re-sorted so the new record sorts to the top of the grid.  However, no method is invoked to update the back-end database.

5. Click the <Reject Changes> button to invoke the DataSet's RejectChanges() method.  Note that the newly added product record disappears.



Experiment No. 2

This experiment demonstrates the action of the DataAdapter.Update() method, in the absence of an active transaction, to store a new record to the Northwind Products table.

Perform the experiment using the following steps:

1. Click the <Load Records from Back-End Database> button to load records from the Northwind Products table and assure yourself that the records in the DataGrid are the same set as currently in the Northwind database.

2. Set the "Update the back-end database" checkbox to True.

3. Set the "Wrap the update in a Transaction" checkbox to False.

4. Click the <Add Record> button.  

A new product record is added to the Products table of the objdsNw DataSet.  The table is re-sorted so that the new record sorts to the top of the grid.

Because you had the "Update the back-end database" checkbox set to True, the Update() method of the DataAdaptor mediating Products records between the back-end database and the objdsNw DataSet is invoked; and the new Product record is inserted into the back-end database.  

5. Click the <Load Records from Back-End Database> button to clear the local DataSet and re-load records from the back-end database. Note that the newly added record remains in the DataGrid, reflecting the fact that it was saved to the back-end database.



Experiment No. 3

This experiment repeats Experiment No. 2, except that this time the add operation takes place inside a transaction. The add is then rolled back, and the records re-loaded to confirm that no record was permanently added.

Perform the experiment using the following steps:

1. Click the <Load Records from Back-End Database> button to load records from the Northwind Products table and assure yourself that the records in the DataGrid are the same set as currently in the Northwind database.

2. Set the "Update the back-end database" checkbox to True.

3. Set the "Wrap the update in a Transaction" checkbox to True as well.

5. Click the <Add Record> button.  

A new product record is added to the Products table of the objdsNw DataSet.  The table is re-sorted so that the new record sorts to the top of the grid.

Because you had the "Update the back-end database" checkbox set to True, the Update() method of the DataAdaptor mediating Products records between the back-end database and the objdsNw DataSet is invoked; and the new Product record is inserted into the back-end database.  

However, you also specified that the update should be wrapped inside a transaction.  The application now prompts you so select the method by which you want to dispose of the transaction.  

6. Select "Roll It Back" from the Select Disposition of Transaction dialog and click <Finish Transaction in Selected Manner>.  The transaction is Rolled Back.

Notice, however, that the added record still appears in the grid, indicating that it remains in the local DataSet 

7. Click the <Reject Changes in DataSet> button.  

Note that the added record still does not disappear. That is because one of the last things the DataAdapter.Update() method does is to invoke the AcceptChanges on the DataSet used as its data source in the update operation.  Therefore the record addition in this experiment was made "permanent" within the DataSet, even though the transaction that inserted it into the back-end database was rolled back.

8. Click the <Load Records from Back-End Database> button to clear the local DataSet and re-load records from the back-end database. Note that now the newly added record *does* disappear from the DataGrid, reflecting the fact that it never made it into the back-end database because of the transaction RollBack.


Experiment No. 4

This experiment repeats Experiment No. 3, except that instead of being rolled back, the add operation is committed to the back-end database.

Perform the experiment using the following steps:

1. Repeat steps 1-5 of Experiment #3.

6. This time, select "Commit It" from the Select Disposition of Transaction dialog and click <Finish Transaction in Selected Manner>.  The transaction is Committed.

7. Click the <Load Records from Back-End Database> button to clear the local DataSet and re-load records from the back-end database. Verify that the newly added record remain in the DataGrid, reflecting the fact that it was permanently added to the back-end database.


Experiment No. 5

This experiment repeats Experiment No. 3, except that instead of rolling back or committing the transaction, the app simply closes the connection hosting the transaction.
Perform the experiment using the following steps:

1. Repeat steps 1-5 of Experiment #3.

6. Select "Close the connection without doing either" from the Select Disposition of Transaction dialog and click <Finish Transaction in Selected Manner>.  The connecton on which the transaction was performed is simply closed, with neither its Commit nor its Rollback method being invoked.

7. Click the <Load Records from Back-End Database> button to clear the local DataSet and re-load records from the back-end database. Note that now the newly added record disappears from the DataGrid, reflecting the fact that it never made it into the back-end database.  Failure to issue a Commit on the transaction before closing the connection has the same result as issuing a RollBack.
