CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2010
    Posts
    2

    Question Update Requires A Valid DeleteCommand Issue

    Heya all,

    I'm pretty new to VB.NET, up until recently I've been an Access developer but sadly there ain't no jobs in it anymore so I'm forced to become a VB.NET developer (to be fair it's about time).

    I'm going through a book which is teaching me some basic coding principles of the language. The current section is detailing how to set up a master child form using two tables linked with a foreign key. The master table is displayed as a number of text boxes, and below is a grid with the child table which shows related records to whatever master record you happen to be checking out. At the top of the screen are the default navigation controls that are added with the grid. The master table is named Person and the child table Book. They are linked using a foreign key and have cascade set up on them.

    The book informed me of the code required under the save icon on the navigation control so that when it is clicked both tables will be updated as required. It is as follows:


    If Validate() Then

    'Make sure editing has completed on both master and child tables
    Me.BookBindingSource.EndEdit()
    Me.PersonBindingSource.EndEdit()

    Try

    'Delete records in child table (Book)
    Me.BookTableAdapter.Update(Me.MyDBDataSet.Book.Select("", "", DataViewRowState.Deleted))

    'Handle add, update and delete in parent table (Person)
    Me.PersonTableAdapter.Update(Me.MyDBDataSet.Person.Select("", "", DataViewRowState.Added Or DataViewRowState.ModifiedCurrent Or DataViewRowState.Deleted))

    'Handle add and update in child table (Book)
    Me.BookTableAdapter.Update(Me.MyDBDataSet.Book.Select("", "", DataViewRowState.Added Or DataViewRowState.ModifiedCurrent))

    Catch ex As Exception

    'Display message if error occurs
    MessageBox.Show(ex.Message)

    End Try

    End If


    The notes were added by me so sorry if I used the wrong terminology in them, I'm just learning the ropes. I have double checked that the code matches that shown, so this should in theory work fine. But if I manually delete a record from the Book (child table) grid and then click the Save icon on the navigation bar it comes up with the error "Update Requires A Valid DeleteCommand when passed DataRow collection with deleted rows".

    Can anyone please inform me why the hell this is coming up and what code has been missed out of the above statement by the authors of the book (and the location/line where this missed code should be added)? I begin to despair when not even the books I purchase allow me to learn the language.

    Can anyone also explain why changes made are not permanently saved to the database? I gather that it's using a "copy" of the data in the database but how do I get the code/form to make changes to the actual underlying database records as well as just the copy? I find this system very unusual, if someone deletes a record in the application why would anyone want this to be just done temporarily?

    Thanks for your help, much appreciated.

  2. #2
    Join Date
    May 2010
    Posts
    2

    Re: Update Requires A Valid DeleteCommand Issue

    I'm using Visual Basic 2008 Express Edition, sorry forgot to mention in initial post

  3. #3
    Join Date
    Dec 2007
    Posts
    234

    Re: Update Requires A Valid DeleteCommand Issue

    In order for it to update the database, it needs to know how to delete rows, as well as insert new ones and update existing ones. So you need to set the DeleteCommand/UpdateCommand/InsertCommand of the Adaptor. There is also a CommandBuilder object that you can use to create the necessary commands for you. It's possible that something got overlooked in the book there. Another way it can be set is through the DataSet wizard, there's a step in there where you can set the Select, Insert, Update and Delete queries.

    -tg

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

Featured