Solved

Deleting a datarow from a table

Posted on 2005-04-11
3
217 Views
Last Modified: 2010-04-07
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
Comment
Question by:scm0sml
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
raterus earned 250 total points
ID: 13754564
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
 

Author Comment

by:scm0sml
ID: 13754951
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
 

Author Comment

by:scm0sml
ID: 13754975
isnt there simply a currentRecord() that could be used to literally just say delete current record?
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now