OleDbConnection gets "External component has thrown an exception."

Anil Kocabiyik picture Anil Kocabiyik · Feb 29, 2016 · Viewed 11.9k times · Source

I' m using a Windows Forms application to export data to excel.

Application is built both x64 and x86.

So both version of Microsoft Access Databse Engine must be installed to work the application on same computer.

Firstly AccessDatabaseEngine.exe is installed and AccessDatabaseEngine_x64.exe is installed passive.

x64 version of application is working

But x86 version is getting

SEHException: "External component has thrown an exception."

 at System.Data.Common.UnsafeNativeMethods.IDBInitializeInitialize.Invoke(IntPtr pThis)
 at System.Data.OleDb.DataSourceWrapper.InitializeAndCreateSession(OleDbConnectionString constr, SessionWrapper& sessionWrapper)
 at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
 at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
 at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup)
 at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
 at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
 at System.Data.OleDb.OleDbConnection.Open()

The code part is below;

var accessConnection = new OleDbConnection(connectionString);
accessConnection.Open();

And ConnectionString is

Provider=Microsoft.ACE.OLEDB.12.0;Data Source="C:\Users\Me\Desktop\ExportTest.xls";Extended Properties="Excel 8.0;HDR=Yes";

How can I solve it?

Answer

Katia picture Katia · Feb 29, 2016

This will usually occur when the build configuration platform in Visual Studio is incorrect, this can occur in both build configuration platforms, x86 and x64.

This is due to a mismatch between the build configuration platform of your project and the Microsoft Access Database Engine which is installed on your machine.

In order to resolve this error:

  • Change the build configuration platform in Visual Studio
  • make sure it matches
  • the Microsoft Access Database Engine version on your machine
  • Recompile and run your project
  • The run time error should now be resolved