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

Article translations Article translations
Article ID: 308650 - View products that this article applies to.
This article was previously published under Q308650
Expand all | Collapse all

On This Page


Managed providers can raise several exceptions. To obtain more detailed information about the cause, you need access to provider-specific error information.


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

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.
  1. Open a new Visual C# .NET Windows Application project.
  2. Open Form1.cs in code, copy the following code, and paste the code at the beginning of the form:
    using System.Data.OleDb;
  3. Copy the following code into the Form1 Load event:
        OleDbConnection cn
            = new OleDbConnection("Provider=SQLOLEDB.1;Data Source=MyWrongServerName");
        catch (OleDbException ex)
            for (int i = 0; i < ex.Errors.Count; i++)
                MessageBox.Show("Index #" + i.ToString() + "\n" +
                    "Message: " + ex.Errors[i].Message + "\n" +
                    "Native: " + ex.Errors[i].NativeError.ToString() + "\n" +
                    "Source: " + ex.Errors[i].Source + "\n" +
                    "SQL: " + ex.Errors[i].SQLState + "\n");
  4. Run the application. A message box should appear after 10 to 15 seconds.


If you have a server named "MyWrongServerName," you may not receive an error.


Fore more information on .NET managed providers, refer to the following MSDN Web site:
Inside .NET Managed Providers


Article ID: 308650 - Last Review: July 15, 2004 - Revision: 3.4
  • Microsoft ADO.NET 1.0
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft ADO.NET 1.1
  • Microsoft Visual C# .NET 2003 Standard Edition
kbhowtomaster kbsystemdata KB308650

Give Feedback


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from