O'Reilly logo

Special Edition Using® Microsoft® ASP.NET by Richard Leinecker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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”) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required