Results 1 to 3 of 3

Thread: [RESOLVED] System.InvalidOperationsException

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    705

    Resolved [RESOLVED] System.InvalidOperationsException

    I am connected to an Access database and using a binding navigator to move through, add, delete and modify records in the data base using Visual Basic 2012. Everything is working fine except for when I want to delete a record which yields the message against the line of code shown in the attached screen shot.
    I have looked and researched and just am not able to figure out exactly what my problem is, and consequently how to correct is. Does anyone have any ideas?
    Attached Images Attached Images  

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: System.InvalidOperationsException

    That error message indicates that at least one of your table adapters doesn't have its DeleteCommand set.

    When you create a typed DataSet, a table adapter is created for each database table you select. The SelectCommand of the table adapter is set to contain a SQL SELECT statement that retrieves each column in the table. The InsertCommand is set to contain a SQL INSERT statement that inserts into each column that is not auto-generated, where auto-generated columns are those like Access AutoNumber and SQL Server identity columns. The UpdateCommand is set to contain a SQL UPDATE statement that updates each column that is not auto-generated based on the primary key. The DeleteCommand is set to contain a SQL DELETE statement that will delete a record based on the primary key.

    The important thing to note here is that the UpdateCommand and DeleteCommand are generated based on the primary key. It has to know which record to update or delete and it's the PK that identifies that record. If there is no PK then there is no way to identify a record so there is no way to know which record to update or delete, so no UpdateCommand or DeleteCommand are generated.

    What you need to do is go back to your database and set a PK on each table and then run the Configuration Wizard on your DataSet to regenerate those table adapters. There's rarely a good reason for a table not to have a primary key so there's almost certainly not a good reason for your table(s) not to have one.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    705

    Re: System.InvalidOperationsException

    I think I get that. I was wondering what good the primary key was, since it would not auto generate the auto number in the form. I had gotten rid of the primary key back in Access. I will put them back in. I use them anyway.
    thanks

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width