Microsoft KB Archive/308043

= How To Obtain Underlying Provider Errors by Using ADO.NET in Visual Basic .NET =

Article ID: 308043

Article Last Modified on 7/15/2004

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft ADO.NET 1.1
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



This article was previously published under Q308043



For a Microsoft Visual C# .NET version of this article, see 308650.

For a Microsoft Visual C++ version of this article, see 308651.



For a Microsoft Visual Basic 6.0 version of this article, see 167957.

This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Data.OleDb

IN THIS TASK
SUMMARY
 * Requirements
 * Steps to Obtain Underlying Provider Errors
 * Troubleshooting

REFERENCES



SUMMARY
Managed providers can raise several exceptions. To obtain more detailed information about the cause, you need access to provider-specific error information. This article dicusses how to obtain the underlying provider errors when using ADO.NET in Visual Basic .NET.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft .NET Framework
 * Microsoft Visual Basic .NET

This article assumes that you are familiar with the following topics:
 * Exceptions
 * Microsoft OLE DB
 * Error handling

back to the top

Steps to Obtain Underlying Provider Errors
To obtain more detailed information about the cause of an exception, wrap your code in a try-catch block, catch the exception, and process the Errors collection from the OleDbException class.  Start Microsoft Visual Studio .NET, and then create a new Windows Application project in Visual Basic .NET. Form1 is created by default.  Open Form1.vb in code, copy the following code, and paste the code at the beginning of the form: Imports System.Data.OleDb   Copy the following code into the Form1 Load event: Dim ex As OleDbException Dim cn As OleDbConnection = New OleDbConnection(&quot;Provider=SQLOLEDB.1;Data Source=MyWrongServerName&quot;) Try cn.Open Catch ex   Dim i As Integer For i = 0 To ex.Errors.Count - 1 MessageBox.Show(&quot;Index #&quot; + i.ToString + ControlChars.Cr _          + &quot;Message: &quot; + ex.Errors(i).Message + ControlChars.Cr _           + &quot;Native: &quot; + ex.Errors(i).NativeError.ToString + ControlChars.Cr _           + &quot;Source: &quot; + ex.Errors(i).Source + ControlChars.Cr _           + &quot;SQL: &quot; + ex.Errors(i).SQLState + ControlChars.Cr) Next i End Try  Run the application. A message box should appear after 10 to 15 seconds.

back to the top

Troubleshooting
If you have a server named &quot;MyWrongServerName,&quot; you may not receive an error.

back to the top

