Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

exception from OLEDB provider 1

Status
Not open for further replies.

seaport

MIS
Jan 5, 2000
923
0
0
US
How do I handle exception from OLEDB provider?

To handle a vb.net exception, I can use
Code:
Catch e As DivideByZeroException

However, all errors from an OLEDB provider lead to only one exception OleDb.OleDbException.

How do I differentiate different errors from OLEDB provider? Right now I am using the message property of the exception. Not sure I am doing it right because the message text may change even from the same type of error. Can I identify the error with an error number?

Seaport
 
catches will be processed in order so if you want to trap oledb exceptions first:
Catch oledbEx as OleDbException
....
Catch ex as exception
....

if the exception is not an OleDbException it will fall to the general exception catch.
If the OleDb exception is like the SqlException, you will have to drill down trhough the inner exceptions to get the true error.
 
Thanks, pmbtech.

I did more tests and found out that I can use OleDbException.ErrorCode to different different errors. For example, I tried to open an Excel file using OleDb and got two different error codes for different errors:
-2147467259 - wrong table name
-2147217904 - wrong column name.

I also found out that the OleDbException exposes a collection of OleDbError objects. So I can use the following code to trap all errors returned from the data provider.
Code:
    Catch ex As OleDb.OleDbException
      Dim x As OleDb.OleDbError
      For Each x In ex.Errors
        Debug.Print(x.NativeError)
        Debug.Print(x.Message)
      Next

Am I on the right track?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top