How To Utilize Database Transactions Within a VB Component From an ASP Page, Part 2By Doug Dean
In Part 2 we looked at the details of the database operation, such as setting up the variables, opening the database connections, and executing a database action. In this part we'll examine how to react to the results of a failed or (hopefully) successful database action!
Committing the Transaction
Our database work is done. The variable acquired in the HTML form has been saved, the record count obtained, increased by one, and also saved. If no errors have occurred, the programming control has arrived safely to this point. It's time to commit out database work for good. This completion is accomplished via the
After the transaction is committed, the database connection is closed.
Setting the objects to
Nothing comes next.
And then we return our new record count to our asp file code and exit the function with the
Rollback if Errors
Should an error interrupt the flow of database processing, a Rollback must be issued so that sections of the database won't be completed piecemeal. Since all errors have been determined to arrive at the
statement, we'll use this as our cleanup area by rolling back the database commands, closing any open database
objects, and raising the error so it will be passed on to the calling code.
THE ASP FILE - How2Project2.asp
Instantiate The VB Object
The ASP page that instantiates our VB object serves double duty by also displaying the HTML form for entering the string value that's placed in our database. The VB object will only be used if there is an incoming string variable issued from our form. Since the VB object and the HTML form are both contained in the same ASP page, a simple
If .. Then statement makes the VB object contingent on whether the asp file called itself
with, or without, a form string value.
The first statement lets the VB object method only be called if there is a form string present for saving to the database.
The HTML FORM
Either way, the HTML code will display the form and return to this same asp page when the summit button is selected. It returns to this page because the ACTION parameter in the FORM tag is set to this asp file's name (
If the VB object method returns the record count, we'll display that too.
Code complete! Happy Programming!