Search This Blog

Tuesday, April 27, 2010

Microsoft.Jet.OLEDB.4.0 provider driver on a 64bit System

Today, I tried to set a connection to an Access 2003 database using ADO.NET  whithin c# 2008 express platform.
After setting the option



Debug -- Exception - Common Language Run Time Excepton - Thrown

This error was thrown

"The Microsoft.Jet.OLEDB.4.0 provider is not registered on the local machine"

After a bit of search, I have realized that the problem was the 32bit OLEDB COM driver that needs to be used in a 32bit process. C# express, by default, on a W64bit system, will compile the project for an x64bit process. To compile for a x86 processor you need:

In visual studio 2008

Tools --> Options --> Projects and Solutions-->General  Check "Show advanced build configurations"

Right click on the Project in the solution explorer folder  - Build - Platform - x86 (the default is Any Cpu)

However c# express does not have this option. You will need to carefully modify the project file using a text or XML editor.

1.    Close the project and/or solution
2.    Select Open File from the File menu
3.    Navigate to the project directory, and highlight the project file
4.    Press the Open button, the project file should open in the XML editor
5.    Locate the first section and add the following line:
x86
6.    Save the project file
7.    Reopen the project and/or solution using Open Project/Solution from the File menu
8.    Continue with development, debugging, and testing


Here you can find the original post by John Wein  which I report here in full for reference

References to 32-bit COM components may not work in VB and C# Applications running on 64-bit platforms

Most existing COM components are only available for 32-bit platforms and will not run in a 64-bit process on a 64-bit platform (although they will run correctly in a 32-bit process on a 64-bit platform). VB and C# applications that reference these 32bit COM components will not run by default on a 64-bit platform because by default the application will launch as a 64-bit process.

The problem appears when a project with one or more COM references is:
1. Migrated to Visual Studio 2005 and executed on 64-bit platforms
-or-
2. Created using Visual Studio 2005 on 64-bit platforms.

In Visual Studio 2005, the VB and C# compilers use the platform target property to determine if the.exe or .dll should run in 32-bit or 64-bit CPU architecture mode. The default setting for this property in Visual Studio 2005 is set to 'AnyCPU', which indicates that the application can run in either 32-bit or 64-bit mode, depending on the host platform. In this situation you may see a message such as "Cannot instantiate class..." when you debug or run these applications.

To resolve this issue
Set the platform target property to 'X86' for your VB or C# projects that have references to COM components.

For C# Projects:
1.    Right click the project in the solution explorer and open 'properties'
2.    Choose the Build tab
3.    Set the Platform Target property to 'X86'

For VB Projects:
1.    Right click the project in the solution explorer and open 'properties'
2.    Choose the Compile tab
3.    Press the Advanced Compile Options... button
4.    Set the Target CPU property to 'X86'

Express Editions:
The VB and C# Express products do not expose the Target property inside the development environment. You will need to carefully modify the project file using a text or XML editor.
1.    Close the project and/or solution
2.    Select Open File from the File menu
3.    Navigate to the project directory, and highlight the project file
4.    Press the Open button, the project file should open in the XML editor
5.    Locate the first section and add the following line:
x86
1.    Save the project file
2.    Reopen the project and/or solution using Open Project/Solution from the File menu
3.    Continue with development, debugging, and testing

Alternatively, if the application is targeted to 64-bit platforms, you can ensure that the COM controls added to the application have 64-bit equivalents on the development and deployment computers.

JohnWein added the following:
Using the above method targets the x86 platform, but it doesn't show the "Configuration:" and "Platform: " boxes on the Properties tabs.  To get this feature, I made a template of one of the projects that shows these boxes.  Now I can target a platform and know what platform I have targeted.

No comments:

Post a Comment