locked
A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll. RRS feed

  • Question

  • I am running 2010 express version of vb.net, access 2010, and win7.  I am trying to test the the linkage between vb.net and access for a programming project.

    I have looked at all of the references for this problem .  One of them talks about changing the advanced compiler options of vb for my little test..  When I follow the instructions, there is no such option to force an x86 assembly from AnyCPU.  From stops put into the sample program, I know it is failing on the open of the database.

    The code fragment I am using is as follows:
    Sub Main()
    ' Try to get VB.net to talk to Access
    Dim cnnDb As New System.Data.OleDb.OleDbConnection
    ' Build connection string
    cnnDb.ConnectionString = _
          "Provider=Microsoft.Jet.Oledb.4.0; Data source=D:\downloads\Mytest.mdb"
    Try
         Stop
         cnnDb.Open()
         Debug.WriteLine("Hey it worked")
    Catch ex As Exception
        Debug.WriteLine("Failed to connect to file")
    Finally
        cnnDb.Close()
    End Try
    Stop
    End Sub

    Appreciate any help..

    Tuesday, March 22, 2011 3:48 PM

Answers

  • Though your answer wasn't exactly the one that fixed the problem, you caused me to look further at the visual basic menus.  I think I am a pretty decent VBA programmer, and I thought Visual Basic is Visual Basic.  The code fragment I was using was taken from one of the MSDN discussions on the problems of connecting Access to Visual Basic.  They said I MUST use Jet as the provider, which I tried with no luck.

    Now I am realizing there is more to this than I thought.  I looked at the tools, connect to database menu and followed my way through.  They use a totally different provider namely: Provider=Microsoft.ACE.OLEDB.12.0

    I changed my code to use that provider and it works like a champ.

    Thanks for the tip.  I see I have a lot to learn.  This is going to be fun.

    By the way, sorry for being in the wrong forum.  Studio is going to be a new experience for me.

     


    • Marked as answer by Chuck Bates Thursday, March 24, 2011 12:19 AM
    Thursday, March 24, 2011 12:17 AM

All replies

  • Hello,

    First of all, your question doesn't seem to be related to Visual Studio Extensibility (VSX), the purpose of this forum (add-ins, packages, SDK, etc.).

    That said, when you compile an app with AnyCPU, when you run it on a Windows 64-bit (which you don't specify), it will run as a 64-bit app, not as a 32-bit app. That means that the references and components that it will try to use will be 64-bit. That's no problem for the .NET Framework, which exists in 32-bit and 64-bit on Windows 64-bit, but it is a problem for certain (most, actually) OLEDB Providers or ODBC drivers, which exist only in 32-bit. If this is your problem, then the solution is to compile the app as 32-bit (x86), not as Any CPU. To do it:

    - Click the Build, Configuration Manager menu

    - In the "Active solution Platform" combobox, select "New...". A "New Solution Platform" dialog is shown

    - In the "Type or select the new platform" combobox, select "x86". Leave the "Copy settings from" combobox with the value "Any CPU" and ensure that the "Create new project platforms" checkbox remains checked. Close the dialog.

    Now, to clean up the "Any CPU" solution platform and project platform:

    - Back to the Configuration Manager dialog, in the "Active solution Platform" combobox, select "Edit...". The "Edit Solution Platforms" dialog is shown.

    - Select the "Any CPU" item and click "Remove". Close the dialog.

    - Back to the Configuration Manager dialog, in the grid for project contexts, "Platform" column, for each project select "Edit...". The "Edit Project Platforms" dialog is shown.

    - Select the "Any CPU" item and click "Remove". Close the dialog.

    That's it. The Configuration Manager is somewhat weird and bad designed from a UI perspective, mixing items and actions in the same combobox and forcing to click "Edit..." before removing an item.

     

     


    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/
    Tuesday, March 22, 2011 4:13 PM
  • Though your answer wasn't exactly the one that fixed the problem, you caused me to look further at the visual basic menus.  I think I am a pretty decent VBA programmer, and I thought Visual Basic is Visual Basic.  The code fragment I was using was taken from one of the MSDN discussions on the problems of connecting Access to Visual Basic.  They said I MUST use Jet as the provider, which I tried with no luck.

    Now I am realizing there is more to this than I thought.  I looked at the tools, connect to database menu and followed my way through.  They use a totally different provider namely: Provider=Microsoft.ACE.OLEDB.12.0

    I changed my code to use that provider and it works like a champ.

    Thanks for the tip.  I see I have a lot to learn.  This is going to be fun.

    By the way, sorry for being in the wrong forum.  Studio is going to be a new experience for me.

     


    • Marked as answer by Chuck Bates Thursday, March 24, 2011 12:19 AM
    Thursday, March 24, 2011 12:17 AM