Catching SQL Errors

Two distinct exception classes exist for catching exceptions, if they are thrown. Depending on which data class you are working with, you will obviously need to use the appropriate exception class. For the examples in this section, we will be using the SqlClient class, so we will use the SqlException class to catch any exception thrown. If you are using the OleDb class, you will need to use the corresponding OleDbException class to catch exceptions.

The SqlException class is designed to handle exceptions that are thrown while executing a SQL statement. When an exception is thrown, you can gather enough information in the SqlException class to figure out what is happening. We have written a small function (see Listings 16.1 ...

Get Debugging 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.