Avatar of debugged
debugged
Flag for United States of America

asked on 

Getting error "Update requires a valid DeleteCommand when passed DataRow collection with deleted rows"

Hi experts, pleease help
My datasource is an mdf file on my machine
I just have a simple form with textboxes and datagridview and buttons to add, delete, edit and save records

Im able to add and save with no problem
When I click on delete, the row gets deleted on the form but when I then click save, I get the error
"Update requires a valid DeleteCommand when passed DataRow collection with deleted rows."

I have primary key setup and tableadapter is configured, advanced options are all checked (although 'refresh' automatically unchecks itself for some reason), fill, return and create dbdirectmethods are checked

One thing I cant find is the acceptchangesduringfill property. I dont know if I even need to tamper with it.
Can someone please help

A screenshot of my tableadapter props is included

Thanks!
Public Class ProjectLogForm
 
    Private WithEvents aBindingSource As BindingSource
    Private aDataSet As ProjLogDatabaseDataSet
    Private aProjLogDataTier As ProjectLogDataTier
    Private addingBoolean As Boolean = False
    Private editingBoolean As Boolean = False
    'Private previousSelectedIndex As Integer
 
    Private Sub ProjectLogForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        Try
 
            'instantiate data tier
            aProjLogDataTier = New ProjectLogDataTier
            aDataSet = aProjLogDataTier.getProjLogData
 
            'declare binding source
            aBindingSource = New BindingSource
            With aBindingSource
                .DataSource = aDataSet
                .DataMember = "ProjLogTable"
                .Sort = "ProjID"
 
                'get correct count of rows in the dataset
                .MoveFirst()
                .MoveLast()
            End With
 
            'bind controls
            With Me
 
                'controls are locked on initial form load
                .lockControls(True)
                .StartDateDateTimePicker.Enabled = False
                .EndDateDateTimePicker.Enabled = False
 
                'projid combobox
                With .ProjIDComboBox
                    .DisplayMember = "ProjID"
                    .DataSource = aBindingSource
                    .DataBindings.Add("text", aBindingSource, "ProjID", False, DataSourceUpdateMode.Never, Nothing)
                End With
 
                'projname textbox
                .ProjNameTextBox.DataBindings.Add("text", aBindingSource, "ProjName")
 
                'gisp textbox
                .GISPTextBox.DataBindings.Add("text", aBindingSource, "GISP")
 
                'bind data grid
                .ProjLogTableDataGridView.DataSource = aBindingSource
 
            End With
 
        Catch ex As Exception
 
            Throw ex
 
        End Try
 
    End Sub
 
    Private Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click
 
        With Me
 
            'specify that its an adding operation
            .addingBoolean = True
 
            'controls are unlocked
            .lockControls(False)
            .StartDateDateTimePicker.Enabled = True
            .EndDateDateTimePicker.Enabled = True
 
            'combobox is set for data input
            .setProjIDComboboxEditing()
 
            'disable navigation, delete and edit buttons
            .lockNavigationButtons(False)
            .saveButton.Enabled = True
            .editButton.Enabled = False
 
            With .aBindingSource
 
                'end any edits
                .EndEdit()
 
                'add record
                .AddNew()
 
            End With
 
            .ProjIDComboBox.Focus()
 
            'display adding in the status bar
            .ToolStripStatusLabel1.Text = "Adding..."
 
            'If .ProjIDComboBox.SelectedIndex <> -1 Then
            '    ' Save the index of the new record for later navigation.
            '    .previousSelectedIndex = .ProjIDComboBox.Items.Count - 1
            'Else
            '    .previousSelectedIndex = 0
            'End If
 
        End With
 
    End Sub
 
    Private Sub saveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveButton.Click
 
        With Me
 
            .addingBoolean = False
            .editingBoolean = False
 
            'lock controls
            .lockControls(True)
            .StartDateDateTimePicker.Enabled = False
            .EndDateDateTimePicker.Enabled = False
 
            'enable navigation and other buttons
            .lockNavigationButtons(True)
            .saveButton.Enabled = False
            .editButton.Enabled = True
 
            'combobox is set for data display
            .setProjIDComboboxEditing()
 
            Try
 
                .Validate()
 
                'end edits
                .aBindingSource.EndEdit()
 
                'update datasource
                .aProjLogDataTier.UpdateDataSource(.aDataSet)
 
                'send changes to dataset
                .aDataSet.AcceptChanges()
 
                'change status text to record(s) saved
                .ToolStripStatusLabel1.Text = "Record(s) Saved"
 
            Catch ex As Exception
 
                Throw ex
 
            End Try
 
        End With
 
    End Sub
 
    Private Sub editButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles editButton.Click
 
        With Me
 
            'unlock textboxes and controls
            .lockControls(False)
            .StartDateDateTimePicker.Enabled = True
            .EndDateDateTimePicker.Enabled = True
 
            .editingBoolean = True
 
            'set combobox for editing
            .setProjIDComboboxEditing()
 
            '.previousSelectedIndex = .ProjIDComboBox.SelectedIndex
 
 
 
        End With
 
    End Sub
 
    Private Sub deleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles deleteButton.Click
 
        Dim deleteDialogResult As DialogResult
 
        Try
 
            deleteDialogResult = MessageBox.Show("Delete Record?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
 
            With Me
 
                If deleteDialogResult = Windows.Forms.DialogResult.Yes Then
 
                    With .aBindingSource
 
                        'end edit
                        .EndEdit()
 
                        'remove record
                        .RemoveCurrent()
 
                    End With
 
                    'update toolstripstatus
                    .ToolStripStatusLabel1.Text = "Record deleted"
 
                Else
 
                    .ToolStripStatusLabel1.Text = String.Empty
 
                End If
 
                'enable navigation
                .lockNavigationButtons(True)
 
                .saveButton.Enabled = True
 
            End With
 
        Catch ex As Exception
 
            Dim msgString As String
            msgString = "Unable to complete Delete" & ex.Message
            MessageBox.Show(msgString, "Delete", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
 
        End Try
 
    End Sub
 
    Private Sub lockControls(ByVal lockControlsBoolean As Boolean)
 
        With Me
            .ProjNameTextBox.ReadOnly = lockControlsBoolean
            .ProjDescTextBox.ReadOnly = lockControlsBoolean
            .RequestorTextBox.ReadOnly = lockControlsBoolean
            .Dept_DivTextBox.ReadOnly = lockControlsBoolean
            .HoursTextBox.ReadOnly = lockControlsBoolean
            .GISPTextBox.ReadOnly = lockControlsBoolean
        End With
 
    End Sub
 
    Private Sub lockNavigationButtons(ByVal lockNav As Boolean)
 
        With Me
            .moveFirstButton.Enabled = lockNav
            .moveLastButton.Enabled = lockNav
            .moveNextButton.Enabled = lockNav
            .movePreviousButton.Enabled = lockNav
        End With
    End Sub
 
    Private Sub setProjIDComboboxEditing()
 
        With Me.ProjIDComboBox
 
            If addingBoolean Or editingBoolean Then
 
                'change combobox style to accept text input
                .DropDownStyle = ComboBoxStyle.Simple
 
                'change datasourceupdate mode
                .DataBindings!text.DataSourceUpdateMode = DataSourceUpdateMode.OnValidation
 
            Else
 
                .DropDownStyle = ComboBoxStyle.DropDownList
                .DataBindings!text.DataSourceUpdateMode = DataSourceUpdateMode.Never
 
            End If
 
        End With
 
    End Sub
 
End Class

Open in new window

tableadapterprops.jpg
Visual Basic.NET

Avatar of undefined
Last Comment
Nasir Razzaq

8/22/2022 - Mon