Update requires a valid UpdateCommand when passed DataRow collection

dougancil

Well-known member
Joined
Jun 29, 2010
Messages
50
Programming Experience
Beginner
I have a form that when a user fires a buttonclick event that I want to be able to capture any edits/changes that he's made to the dataset that fills a tableadapter. Here is the code I have currently:

VB.NET:
Public Class ExceptionEdit
    Private Sub ExceptionEdit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'MDRDataSet.scratchpad3' table. You can move, or remove it, as needed.
        Me.Scratchpad3TableAdapter.Fill(Me.MDRDataSet.scratchpad3)
        Exceptioncopy()
    End Sub
    Private Sub SaveExceptionButton(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveexceptionsButton.Click
        Try
            Me.Validate()
            Me.Scratchpad3BindingSource.EndEdit()
            Me.Scratchpad3TableAdapter.Update(Me.MDRDataSet.scratchpad3)
            MsgBox("Update successful")
        Catch ex As Exception
            MsgBox("Update failed")
        End Try
        Me.Close()
        ExceptionsFinal.Show()
    End Sub

and when I debug this, I get an error when I reach this line of code:
VB.NET:
Me.Scratchpad3TableAdapter.Update(Me.MDRDataSet.scratchpad3)
the error being this:

Update requires a valid UpdateCommand when passed DataRow collection with modified rows.

The table does have a primary key, and I've seen a lot of people talk about refreshing the data table to make this work, but because am using a temp database, I'd have to programmatically put that in my code somewhere, or is that not the problem at all?

Thank you
Doug
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,717
Location
Sydney, Australia
Programming Experience
10+
Let me guess: you created the database without a primary key in that table, generated the Data Source in your project, then you added a primary key to your database table, correct? The database table had no primary key when the Data Source was generated so the TableAdapter has no UpdateCommand. Adding a primary key to the database table doesn't make any changes to the TableAdapter. you have to regenerate the Data Source so that it goes back to the database and uses the new schema to recreate the TableAdapters and add an UpdateCommand. There's a button in the Data Sources window to do just that.
 
Top Bottom