Microsoft Knowledge Base Email Alertz

(308043) - 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...

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 308043 - Last Review: July 15, 2004 - Revision: 2.3

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

This article was previously published under Q308043

On This Page

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.


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

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. Start Microsoft Visual Studio .NET, and then create a new Windows Application project in Visual Basic .NET. Form1 is created by default.
  2. Open Form1.vb in code, copy the following code, and paste the code at the beginning of the form:
    Imports System.Data.OleDb
    					
  3. Copy the following code into the Form1 Load event:
    Dim ex As OleDbException
    Dim cn As OleDbConnection = New OleDbConnection("Provider=SQLOLEDB.1;Data Source=MyWrongServerName")
    Try
        cn.Open()
    Catch ex
        Dim i As Integer
        For i = 0 To ex.Errors.Count - 1
            MessageBox.Show("Index #" + i.ToString() + ControlChars.Cr _
               + "Message: " + ex.Errors(i).Message + ControlChars.Cr _
               + "Native: " + ex.Errors(i).NativeError.ToString() + ControlChars.Cr _
               + "Source: " + ex.Errors(i).Source + ControlChars.Cr _
               + "SQL: " + ex.Errors(i).SQLState + ControlChars.Cr)
        Next i
    End Try
    					
  4. Run the application. A message box should appear after 10 to 15 seconds.

Troubleshooting

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


REFERENCES

For more information on .NET managed providers, refer to the following MSDN Web site:
Inside .NET Managed Providers
http://msdn.microsoft.com/en-us/library/ms810268.aspx (http://msdn.microsoft.com/en-us/library/ms810268.aspx)

APPLIES TO
  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft ADO.NET 1.1
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
Keywords: 
kbhowtomaster kbsystemdata KB308043
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate