Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Deleting a datarow from a table

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

837 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