One of the most improved parts of VB .NET is the error handling. This is now coded in a manner similar to Java and C++ and is generally known as "Try-Catch" error handling. The idea is that sections of code that might experience critical errors are placed in a Try code block. Then, if an error does occur, a Catch code block will process the error. This allows much more specific error handling to be done. (It's often said that Microsoft never invented anything but they sure know how to steal the best ideas!)
Since the section of code that actually reads the database is critical, the entire section will be in a Try block. If an error occurs, the message will be displayed in the form using code in the Catch block. A more complete and professional system would probably have more error checking, but it would be variations of this same technique.
To see the Try-Catch code in action, try changing the name of the Access database table from Articles to something incorrect - say, "George" and run the program again. You should see something like this:
Here's the ADO .NET code (see the end of the article for complete code) down to the execution of the DataReader. The CommandText is set to a very simple SQL statement that reads everything in the Articles table from the Access database.
ocmdAboutVBCommand = New OleDbCommand()
With ocmdAboutVBCommand
.Connection = New OleDbConnection(strAboutVBConn)
.Connection.Open()
.CommandText = "Select * From Articles"
odtrAboutVBDataReader = .ExecuteReader()
End With
After the DataReader has been executed, the results of the query are available using the Read method of the DataReader. Each time the Read method is called, a new row of the query results are provided. To transfer all of the results into our arrays, the Read method is called until it returns False. Perfect for a Do-While loop.
One of the advantages of the .NET Foundation is the wealth of built in functions available in the objects.
For example, since we're going for efficiency in this example, we will use the BeginUpdate and EndUpdate methods of the ListBox control. These methods allow the ListBox to be conveniently updated without being repainted on the screen each time a change is made.
ADO .NET features another performance option. Let's look at just the artDate field of the database to see how it works. In the old ADO, you would probably access the actual columns in the old ADO RecordSet (it's not in ADO .NET) with a statement similar to this new ADO .NET statement where the column is referenced by name.
dteAboutVBDate(I) = odtrAboutVBDataReader.Item("artDate")
Next page >
Finishing the Application > Page
1,
2,
3,
4,
5,
6