Microsoft KB Archive/308651

= How to obtain underlying provider errors by using ADO.NET in Visual C++ .NET =

Article ID: 308651

Article Last Modified on 8/31/2005

-

APPLIES TO


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

-



This article was previously published under Q308651





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



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

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



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 shows how to gather this information.

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
 * Microsoft .NET Framework
 * Microsoft Visual C++ .NET

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

Technique description
You must wrap your code in a &quot;try... catch&quot; block to catch the exception and process the error collection from the OLEDBException.

Create project and add code
 Create a new Managed C++ application. Name the project ProviderError .  Open the ProviderError.CPP file. Copy the following code and paste it in to replace the default code built in by Microsoft Visual Studio .NET:
 * 1) include &quot;stdafx.h&quot;
 * 2) using 
 * 3) using 
 * 4) using 

using namespace System; using namespace System::Data::OleDb;

// This is the entry point for this application int wmain(void) int main(void) {       OleDbConnection *myCn = new OleDbConnection(&quot;Provider=SQLOLEDB.1;Data Source=MyWrongServerName&quot;); try {           myCn->Open; }       catch(OleDbException *ex) {           Int32 i;            for (i = 0; i <= ex->Errors->Count - 1; i++) {               Console::WriteLine(&quot;Index # {0} \n Message: {1} \n Native: {2}&quot;                     &quot;\n Source: {3} \n SQL: {4} \n&quot;, i.ToString, ex->Errors->Item[i]->Message,                     ex->Errors->Item[i]->NativeError.ToString, ex->Errors->Item[i]->Source,                     ex->Errors->Item[i]->SQLState); }   }
 * 1) ifdef _UNICODE
 * 1) else
 * 1) endif

return 0; }  On the Debug menu, click Run without Debugging to run the application. A console window that contains the provider error should appear after about 10 to 15 seconds.

Troubleshooting
If you have a server called &quot;MyWrongServerName&quot;, you may not get an error message.

Keywords: kbhowtomaster kbsystemdata KB308651

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.