locked
VB 2008 dataset error "A first chance exception of type 'System.InvalidOperationException' occurred in System.Data.dll" RRS feed

  • Question

  • I am trying to perform a simple bound connection between an Access database and a text box on a form.  This is actually Chapter 18 of the "Microsoft Visual Basic 2008" book.

    I get the same error message when trying to create my own project "MY ADO Form" or the "ADO Form" project supplied from the DVD provided with the book.  I am running the 90 Day Trial Version of Visual Studio 2008 with SP1 and Net 3.5 SP1.  My operating system is the 64 bit Version of Vista with all updates.

    The error when I try to run the application in the debug mode is:

    "A first chance exception of type 'System.InvalidOperationException' occurred in System.Data.dll"

    and as a result no data shows up in the text box from the database and the data selection bar is grayed out in the form.  The underlying database "Students Data Set" appears to be fine as I have seen the correct data for the fields "Instructor" and "PhoneNumber" prior to running the program and when connecting with the database.

    Any help would be appreciated.

    JV

    Saturday, September 27, 2008 4:35 AM

Answers

  • The actual cause of this problem, as I later found out was because I am running a 64 bit version of the Vista Operating System.

    The setup for Visual Studio has to be changed to "x86" from "AnyCPU".  Within Visual Studio 2008 it is done as follows:

    'Notes: In order for this to work on a 64 bit Vista Operating System the processor must be an X86

    ' This is apparently because the Microsoft.Jet.OLEDB.4.0 only works on a 32 bit system

    ' - Select "Project"

    ' - Select "Properties" for your project

    ' - Select "Compile"

    ' - Select "Advanced Compile Options"

    ' - Select "x86" instead of "AnyCPU"


    JV

    Friday, October 3, 2008 11:47 PM

All replies

  •  

    JVDEV said:

    The error when I try to run the application in the debug mode is:
    "A first chance exception of type 'System.InvalidOperationException' occurred in System.Data.dll"

    Hi JVDEV,

    1. Please make sure that you didn't use any reserved words as database table field name. If did, you either add square brackets to both sides or rename it.

       Similar issue: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=854254&SiteID=1

      List of reserved words in Jet 4.0
    http://support.microsoft.com/kb/248738

      References:
    http://support.microsoft.com/kb/892608


    2. You also can binding TextBox to DataSource in code like this:
       Me.TextBox1.DataBindings.Add(New Binding("Text", Me.BindingSource1, "Field1", True
    ))


    3. Here are four choices for you to make simple Data Access application(Next, Previous, First, Last, Update, Delete, Insert, Save
    ).

    Method 1: Update (Insert/Update/Delete) data back into MS Access database from DataGridView.
    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2758290&SiteID=1

    Imports System.Data.OleDb
    Public Class Form1
    Dim myDA As OleDbDataAdapter
    Dim myDataSet As DataSet
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=|DataDirectory|\myDB.mdb")
    Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM Table1", con)
    con.Open()
    myDA = New OleDbDataAdapter(cmd)
    'Automatically generates DeleteCommand,UpdateCommand and InsertCommand for DataAdapter object
    Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(myDA)
    myDataSet = New DataSet()
    myDA.Fill(myDataSet, "MyTable")
    DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView
    con.Close()
    con = Nothing
    End Sub
    ' Save data back into database
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.Validate()
    Me.myDA.Update(Me.myDataSet.Tables("MyTable"))
    Me.myDataSet.AcceptChanges()
    End Sub
    End Class


    Method
    2: Execute
    Select/Insert/Delete/Update T-SQL commands in VB.NET code

    Code sample: How to Select/Insert/Delete/Update records in MS Access database in VB.NET
    http://forums.msdn.microsoft.com/en-US/vbgeneral/thread/87913f28-992d-4705-963b-cb0ffa53d8dd/
    Code sample: How to Select/Insert/Delete/Update records in SQL Server database in VB.NET
    http://forums.msdn.microsoft.com/en-US/vbgeneral/thread/89e1067d-16e7-44e8-b12d-d78845bf255f/


    Method 3.
    Using Data Wizard with BindingNavigator control.
    Please check the 11th post in this thread for detailed walkthrough
    :
    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2471782&SiteID=1



    Method 4
    .
    Using DataSet/DataTable/DataAdapter in VB.NET code
    Please check the 12th post and 13th post in this thread for detailed code sample:
    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2471782&SiteID=1



    Note: If you add database file to your project via Add Data Source Wizard, you need to: highlight the database in Solution Explorer -> right-click on your database file -> select Properties -> change the "copy to ouput directory" property value to "copy if newer". Otherwise, if you restart your application, the database seems not to be updated.
    Detail:
    http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/c911fcd1-d9f5-4605-9c1e-0e046810009d



    Best regards,
    Martin Xie

    Friday, October 3, 2008 6:03 AM
  • The actual cause of this problem, as I later found out was because I am running a 64 bit version of the Vista Operating System.

    The setup for Visual Studio has to be changed to "x86" from "AnyCPU".  Within Visual Studio 2008 it is done as follows:

    'Notes: In order for this to work on a 64 bit Vista Operating System the processor must be an X86

    ' This is apparently because the Microsoft.Jet.OLEDB.4.0 only works on a 32 bit system

    ' - Select "Project"

    ' - Select "Properties" for your project

    ' - Select "Compile"

    ' - Select "Advanced Compile Options"

    ' - Select "x86" instead of "AnyCPU"


    JV

    Friday, October 3, 2008 11:47 PM
  • Hi JVDEV,


    I’m glad to hear that you found the cause.
    Thank you for sharing your experience here.
    It will be very beneficial
    for other community members having the similar issues. 


    Regards,
    Martin

    Monday, October 6, 2008 4:53 AM
  • JVDEV
    Thank You.  By posting this you helped me solve a very similar problem.  Thank You!
    Website Developer
    Wednesday, November 5, 2008 4:00 PM
  • Nice JVDEV! Thank you very much, I've been looking for 3 days trying to figure out why my database couldn't connect and this was it! I can't say thank you enough! Thank you!
    Wednesday, April 29, 2009 7:19 PM
  • JVDEV,

    Same book, same problem on 64 bit Vista.  Since I cannot find any errata from Microsoft Press, you have provided a valuable solution!

    Thank you.
    Saturday, May 30, 2009 6:49 PM
  • Different book but same problem.

    Thanks for the answer.
    Monday, July 6, 2009 4:49 AM
  • Hi, I just stumbled across this forum with the database and vista 64bit issue. I had the same exact problem with vista 64bit. When I took the same project over to my laptop which runs xp32bit, problem was gone. I could navigate, update and add/commit all in the text boxes.However, all this was done with the northwind database which is an old one with the extension of .mdb! I then wanted to change the column names of that table, but it wouldn't let me unless I'd convert it over to the newer version which is access2007. I did so, changed the names on the columns and saved the table back to the project. Only to find out that now the provider has changed. Its not jet.oledb.4.0 anymore, rather microsoft.ace.oledb.12.0. Now when i run the program it still navigates through the records in my text boxes but the update and add/commit buttons won't work anymore. It gives me a syntax error of "INPUT INTO". Any Ideas why the new data format ace.oledb.12.0 access database won't take the code anymore? And what other code can i use?
    Thanks for the help.
    learningvb8
    Sunday, July 19, 2009 6:53 AM
  • The actual cause of this problem, as I later found out was because I am running a 64 bit version of the Vista Operating System.

    The setup for Visual Studio has to be changed to "x86" from "AnyCPU".  Within Visual Studio 2008 it is done as follows:

    'Notes: In order for this to work on a 64 bit Vista Operating System the processor must be an X86

    ' This is apparently because the Microsoft.Jet.OLEDB.4.0 only works on a 32 bit system

    ' - Select "Project"

    ' - Select "Properties" for your project

    ' - Select "Compile"

    ' - Select "Advanced Compile Options"

    ' - Select "x86" instead of "AnyCPU"


    JV

    I am running VB Express 2008 and Vista 64bit, I tried to do these steps but do not have an option to change CPU to x86, i only have "AnyCPU"

    Any ideas?
    • Proposed as answer by asbtek Wednesday, July 29, 2009 6:02 PM
    • Unproposed as answer by Martin Xie - MSFT Thursday, July 30, 2009 3:41 AM
    • Proposed as answer by Novice2Pro Saturday, August 1, 2009 11:58 PM
    Tuesday, July 21, 2009 11:54 AM
  • To remedy in VB Express 2008, you'll have to modify the Project File (.vbproj) file in Vim or Notepad.

    Insert the following line of text above the </PropertyGroup>:

    <PlatformTarget>x86</PlatformTarget>

    This should resolve issues compiling in the Vista x64 bit environment.

    BTW - Accidentally proposed as answer instead of replying -- sorry for any confusion.
    • Proposed as answer by 10consult Thursday, March 4, 2010 10:33 AM
    Wednesday, July 29, 2009 6:05 PM
  • ippy98,

    I had similar issue, please try to look if you are opening a datareader (or similar) which is already open, if so trying closing and re-open. This worked for me, might as well for you, if not so buzz here so that can try something else.

    Running here VS 2008 Exp on VHP-64bit

    Sorry proposed as answer while trying to reply (first timer here !)

    Happy programming.


    Sunday, August 2, 2009 12:04 AM
  • This is THE best description and answer for this problem that I have found. I have looked EVERYWHERE to solve this problem. This also works if you're running the 64 bit Windows 7.

    Thanks!
    Thursday, November 19, 2009 5:51 PM
  • Thank you for posting this; I was convinced I was doing something stupid.

    Well done.

    Wednesday, January 13, 2010 4:22 PM
  • At least by not asking questions in a closed question thread

    Simply state a new one
    Success
    Cor
    Saturday, January 16, 2010 11:42 AM
  • I have the same problem running express 08 on Windows 7 x64, however I don't have any option to change CPU.

    Any idieas??

    Friday, February 12, 2010 6:38 AM
  • Thanks for this information.  I am running VB Express 2008 on Windows 7 64-bit.  This worked perfectly for me as well.  I did guess that the "</PropertyGroup>" that you were speaking of was the first in the file (it appears a number of times in my .proj file).
    Friday, April 9, 2010 7:39 PM
  • You're so great.

    Open *.vbpeoj with Notepad and add <PlatformTarget>x86</PlatformTarget> below the first paragraph <PropertyGroup>.

    For example:

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup>
        <PlatformTarget>x86</PlatformTarget>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <ProductVersion>9.0.21022</ProductVersion>
        <SchemaVersion>2.0</SchemaVersion>
        <ProjectGuid>{D3E765D2-AAD7-4D1E-9368-45E678592B32}</ProjectGuid>
        <OutputType>WinExe</OutputType>
        <StartupObject>SV_0883016_LTQL1_BT4.My.MyApplication</StartupObject>
        <RootNamespace>SV_0883016_LTQL1_BT4</RootNamespace>
        <AssemblyName>SV_0883016_LTQL1_BT4</AssemblyName>
        <FileAlignment>512</FileAlignment>
        <MyType>WindowsForms</MyType>
        <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
        <OptionExplicit>On</OptionExplicit>
        <OptionCompare>Binary</OptionCompare>
        <OptionStrict>Off</OptionStrict>
        <OptionInfer>On</OptionInfer>
      </PropertyGroup> 

    I've just solved it. Best wishes and good luck. Thanks

    Wednesday, August 4, 2010 12:09 PM
  • Hi,

    I am using Method 1 suggested by you. I am trying to save the data using Textboxes but it is not letting me. The option me.bindingsource1 doesnt come. What to do??? Please help...

    Tuesday, August 10, 2010 7:10 AM
  • Thank you for solving this problem and posting the solution. I would have spent a few days on this otherwise, you have saved me a lot of time and effort. I to am working on Chapter 18 of Visual Basic 2008 Step by Step and running Vista 64.
    • Edited by jallan1 Friday, November 26, 2010 1:17 AM Incomplete.
    Friday, November 26, 2010 1:14 AM
  • I had the exact same problem, and just used your recomendation, and it worked

     

    I confirm that this is the correct answer

    Wednesday, February 9, 2011 8:17 PM
  • Thank you JVDEV for your response to this thread. I have been going nuts trying to figure this out on my own. Finally, I turned to the Internet and your solution solved my issues immediately! I don't think I would have ever figured this out on my own, thanks again.
    Tuesday, April 19, 2011 4:38 AM
  • Ihad the same problem but MSDN library could not help. When I tried your method, it work perfectly well. My DataGridView Control was populated with records immediately. Note that the same thing also applied to Windows 7 and not only Vista. It is not only Microsoft.Jet.OLEDB.4.0 but also on other databases like Oracle and SQL Server

    The solution was stated as shown bellow.

    'Notes: In order for this to work on a 64 bit Vista Operating System the processor must be an X86

    ' - Select "Project"

    ' - Select "Properties" for your project

    ' - Select "Compile"

    ' - Select "Advanced Compile Options"

    ' - Select "x86" instead of "AnyCPU"

    Wednesday, August 3, 2011 12:15 PM
  • Thanks JVDEV, I was also having same issue and our IT guys have no solution to it. It worked like a charm.

    Thursday, September 29, 2011 11:46 PM
  • Hi im using windows 7 and visual basic 2008, im having the same problem. Ive tried this but there isnt an option at all that says any cpu or x86?? Would appreciate any help.  Thanks
    Saturday, October 1, 2011 8:02 PM
  • HUmm.. had a long day ... going through this .. ops !! ahh its a miracle i guess worked in sec X64 sucks creates a lot of prob @ d time of code execuation ... thnx for the tut worked out well for me :)

    Thursday, May 3, 2012 4:39 PM
  • Hi All, I think I have the same problem but I am working with VS 2010 instead of 2008 and am running 64-bit Windows Vista too, but this solution of inserting the "<PlatformTarget>x86</PlatformTarget>" with Notepad into my .csproj file has not worked for me; my WCF-project still seems to have a problem when running in debug-mode and will not retrieve data which I CAN retrieve with the same LINQ-query in Linqpad. Wcf shows the following in the output window: 

    A first chance exception of type 'System.NotSupportedException' occurred in System.Data.Entity.dll
    The thread '<No Name>' (0x1b50) has exited with code 0 (0x0).
    The program '[7040] WCFTestClient.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).....................................

    Below an example of a Method that WILL return data in Visual Studio WcFTestClient:

    public List<WShortDeal> Test()
            {
                try
                {
                    return entities.ProductInstance_Deal
                        .Select(d => new WShortDeal()
                        {
                            Id = d.Deal.Id
                           // ,
                           // Name = d.Deal.Deal_Language.SingleOrDefault(l => l.Language.Id == 1).Name
                            ,
                            NewPrice = (double)d.ProductInstance.Price * (1 - d.Deal.SalesPercentage / 100)
                            ,
                            OldPrice = (double)d.ProductInstance.Price
                            ,
                            Valuta = d.ProductInstance.Valuta.ValutaCode
                            ,
                            Type = d.Deal.Type
                            ,
                            IdCompanyAccount = d.ProductInstance.IdCompanyAccount
                        })
                            .ToList();
                }
                catch (Exception)
                {
                    return null;
                }
            }

    But if I uncomment the part that's BOLD, it will NOT return data anymore?!!!!! In LinqPad this query WILL RETRIEVE DATA though!!

    Could someone please help? Is there another solution for my problem? I've been struggling with this problem for days now :(




    Tuesday, July 17, 2012 8:44 AM
  • Really Thanks Sir! You Saved My Time! Thank You!! :)
    Wednesday, March 25, 2015 10:05 AM