Error Handling

I have shown a lot of code examples without any support for trapping errors, which is not an ideal way to program in real life. There are numerous ways a connection or command execution attempts can fail, all of which need to be handled correctly. Listings 9.29 and 9.30 show the DataReader example with the correct error-handling structure applied.

Listing 9.29. Using the DataReader—Visual Basic Example
 Import System.Data Import System.Data.SqlClient Dim myConnection as new SQLConnection(“server=localhost;uid=sa;pwd=; database=pubs”) Dim myCommand as new SQLCommand(“select * from Publishers”,myConnection) Dim myReader as SQLDatareader Try myConnection.Open() myReader=myCommand.ExecuteReader() While (myReader.Read) Response.Write(myreader(“pub_name”) ...

Get Special Edition Using® Microsoft® ASP.NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.