• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

Deleting a datarow from a table

Can sum1 tell me whats wrong with this please....

 Private Sub DeleteTitle()
        Try
            Dim DeleteCmdBuilder As New OleDbCommandBuilder(myDataAdapter)
            Dim mydeleterow = myDataTable.Rows
            myDataAdapter.Fill(myDataTable)

            For Each mydeleterow In myDataTable.Rows
                If mydeleterow("strJobTypeTAG") = cmbSrcTitle.Text Then
                    mydeleterow.delete()
                    myDataTable.AcceptChanges()
                    myDataAdapter.Update(myDataTable)
                End If
            Next             '-----------------------------------------------------------------------------

        Catch ex As OleDbException
            MsgBox(ex.ToString)
        End Try
    End Sub

I am trying to delete a row where the text in a combo box is the same as a field in the current row of the datatable.
0
scm0sml
Asked:
scm0sml
  • 2
1 Solution
 
raterusCommented:
You should not be calling .AcceptChanges, right after you call .delete().  When the dataadapter goes to update any changes, it won't see this change and skip over it.  Also, you should not be running .Update(myDataTable) on a row by row basis.  Delete all the rows in the datatable first, then .Update.  There is no sense, other than wasting CPU cycles to do it row by row.  The overall logic should be like this...

            For Each mydeleterow In myDataTable.Rows
                If mydeleterow("strJobTypeTAG") = cmbSrcTitle.Text Then
                    mydeleterow.delete()
                End If
            Next             '-----------------------------------------------------------------------------

            myDataAdapter.Update(myDataTable)
            myDataTable.AcceptChanges()
0
 
scm0smlAuthor Commented:
I'm getting concurrency violation: the delete command affected 0 records.

I've got this code:

Private Sub DeleteTitle()
        Try
            Dim DeleteCmdBuilder As New OleDbCommandBuilder(myDataAdapter)
            Dim mydeleterow = myDataTable.Rows
            myDataAdapter.Fill(myDataTable)

            For Each mydeleterow In myDataTable.Rows
                If mydeleterow("strJobTypeTAG") = cmbSrcTitle.Text Then
                    mydeleterow.delete()
                End If
            Next

            myDataAdapter.Update(myDataTable)
            myDataTable.AcceptChanges()

            End
        Catch e As Exception
            MsgBox(e.ToString)
        Catch ex As OleDbException
            MsgBox(ex.ToString)
        End Try
    End Sub

The record is actually being deleted but then this message is appearing.
Any ideas?
0
 
scm0smlAuthor Commented:
isnt there simply a currentRecord() that could be used to literally just say delete current record?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now