Getting Started column, "Keep Data Consistent With Transactions," by Paul Delcogliano
Visual Studio Magazine, May 2004

*** This document describes how to set up the sample application
*** A description of how to run the application is included below.
*** PLEASE NOTE: Two versions of the sample application are provided. One written in VB.Net and a second written in C#. 
*** Use the setup instructions below that are appropriate to the version of the sample you want to install.


*** SETUP FOR VB.NET sAMPLE APPLICATION ***
1 - copy the TransactionDemo root folder from the zip file to your harddrive. This will copy three projects to your pc. The windows application project TransactionDemo, the business logic project, TransactionDemoBusinessLogic and the data access project, TransactionDemoDataAccess. It will also copy the solution file, TransactionDemo.sln to your pc. The solution file will be located in the TransactionDemo windows project sub-folder.

2 - Create a new folder under InetPub\wwwroot and name it TransactionDemoWebService.

3 - Copy the files in the WebService\TransactionDemoWebService folder from the zip file to the newly created TransactionDemoWebService folder located under your InetPub\wwwroot folder.

4 - Open the Internet Information Services application (located in the Administrative Tools program group on Win XP Pro). Expand the "(local computer)" node, then expand the "Default Web Site" node in the treeview. Navigate to the "TransactionDemoWebService" folder. Highlight the folder and then right click on the folder. From the context menu, click the "Properties" menu option. This will open a dialog with several tabs. The "Directory" tab should be selected, if it is not, click on the "Directory" tab. On the bottom of the dialog, in the "Application Settings" section click the button labeled "create".  Then click "OK" to accept the changes and close the dialog. Close the Internet Information Services manager.

5 - Extract the StoredProcedures.sql file from the zip file. Run this script in the Northwind database. This script sets up all of the stored procedures needed by the sample application.

6 - Open and run the TransactionDemo VB.Net solution file, TransactionDemo.sln


*** SETUP FOR C# sAMPLE APPLICATION ***
1 - copy the TransactionDemoC_Sharp root folder from the zip file to your harddrive. This will copy three projects to your pc. The windows application project TransactionDemoC_Sharp, the business logic project, BusinessLogic and the data access project, DataAccess. It will also copy the solution file, TransactionDemoC_Sharp.sln to your pc. The solution file will be located in the TransactionDemoC_Sharp windows project folder.

2 - Create a new folder under InetPub\wwwroot and name it TransactionDemoCSharpWebService.

3 - Copy the files in the WebService\TransactionDemoCSharpWebService folder from the zip file to the newly created TransactionDemoCSharpWebService folder located under your InetPub\wwwroot folder.

4 - Open the Internet Information Services application (located in the Administrative Tools program group on Win XP Pro). Expand the "(local computer)" node, then expand the "Default Web Site" node in the treeview. Navigate to the "TransactionDemoCSharpWebService" folder. Highlight the folder and then right click on the folder. From the context menu, click the "Properties" menu option. This will open a dialog with several tabs. The "Directory" tab should be selected, if it is not, click on the "Directory" tab. On the bottom of the dialog, in the "Application Settings" section click the button labeled "create".  Then click "OK" to accept the changes and close the dialog. Close the Internet Information Services manager.

5 - Extract the StoredProcedures.sql file from the zip file. Run this script in the Northwind database. This script sets up all of the stored procedures needed by the sample application.

6 - Open and run the TransactionDemo C# solution file, TransactionDemoC_Sharp.sln



*** DEMO INSTRUCTIONS ***
The sample application simulates a point of sale form. When the application starts, you will see a form where you can select a customer from a drop down. A grid appears below the customer drop down listing all of the products in the Northwind database. To see how a transaction works, select a customer from the drop down, then enter a value > 0 and less than the number of "units in stock" in the "requested quantity" column of the grid next to a product that the customer wants to purchase.  

On the bottom of the sample form, there is a list box and three buttons. Clicking one of the buttons will simulate a sale of the requested product to the selected customer. The application is designed to "fail" 40% of the time. The application simulates and invalid credit card for the selected customer 40% of the time. When an invalid card is found, the transaction rolls back the changes made to the database.

In a successful transaction, the application will create new order, and order detail records and adjust inventory to show the sale of the requested amount of the product. When an error occurs, or an invalid credit card is found, the transaction rolls back the changes made to the database for that particular transaction. Note that each product executes as a separate transaction, so if you enter a requested amount next to 3 different products, 3 independent transactions are created, some of which may execute successfully, some of which may not. The listbox in the lower left hand corner of the form shows you which transactions execute successfully, and which do not. For those that do not, the value for the "units in stock" column will not decrease.

Each of the three buttons demonstrates a different technique (as described in the article) for implementing transactions. The "Manual" button creates a transaction using ADO.NET and the SQLClient class. The "Automatic" button creates an automatic transaction using a custom .Net class. It also explicitly calls SetComplete() or SetAbort() to commit or rollback the transaction instead of using the AutoComplete attribute. The "Web" button executes the same code as the "Automatic" button, but does so by calling a method of a Web service.



