Microsoft KB Archive/308650

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

Article ID: 308650

Article Last Modified on 7/15/2004

-

APPLIES TO


 * Microsoft ADO.NET 1.0
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft ADO.NET 1.1
 * Microsoft Visual C# .NET 2003 Standard Edition

-



This article was previously published under Q308650



For a Microsoft Visual Basic .NET version of this article, see 308043.

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

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.

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 C# .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.  Open a new Visual C# .NET Windows Application project.  Open Form1.cs in code, copy the following code, and paste the code at the beginning of the form: using System.Data.OleDb;   Copy the following code into the Form1 Load event: {   OleDbConnection cn        = new OleDbConnection(&quot;Provider=SQLOLEDB.1;Data Source=MyWrongServerName&quot;); try {       cn.Open; }   catch (OleDbException ex) {       for (int i = 0; i < ex.Errors.Count; i++) {           MessageBox.Show(&quot;Index #&quot; + i.ToString + &quot;\n&quot; +                &quot;Message: &quot; + ex.Errors[i].Message + &quot;\n&quot; +                &quot;Native: &quot; + ex.Errors[i].NativeError.ToString + &quot;\n&quot; +                &quot;Source: &quot; + ex.Errors[i].Source + &quot;\n&quot; +                &quot;SQL: &quot; + ex.Errors[i].SQLState + &quot;\n&quot;); }   } }                     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

