Solved

Deleting a datarow from a table

Posted on 2005-04-11
3
215 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

11 Experts available now in Live!

Get 1:1 Help Now