INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

DataTable Delete() method inconsistent

DataTable Delete() method inconsistent

(OP)
I have 3 tables that I use temporarily to display search results in a DataGridView.  At the start of each search, I delete all of the rows and then save the search results.  For the first 2 tables, this process works entirely as expected, but for the 3rd table, the delete only works the first time in a session, leaving all rows in the table for subsequent searches.

Here is the code for one of the 2 successful routines:

CODE

    Private Sub SaveMaintenanceFileSearchResults()
        Dim Row As Integer = 0

        Dim strSQL As String = "SELECT Structure, DocumentDate, Category, ID FROM MaintenanceFileSearchResults ORDER BY Structure, DocumentDate"
        Dim Adapter As OleDb.OleDbDataAdapter = _
            DataLayer.GetDataAdapter(cMaintenanceManualsConnection, _
                                     strSQL)
        Dim MyTable As DataTable = _
            DataLayer.GetDataTable(Adapter, _
                                   cMaintenanceManualsConnection, _
                                   strSQL, _
                                   True)
        Dim Builder As OleDbCommandBuilder = New OleDbCommandBuilder(Adapter)

        Dim NewRow As DataRow

        With MyTable
            Adapter.DeleteCommand = Builder.GetDeleteCommand
            For Each NewRow In .Rows
                NewRow.Delete()
            Next NewRow
            Adapter.Update(MyTable)


            For Row = 0 To BDMDSSearchArrays.NumMaintenanceFiles
                NewRow = .NewRow
                NewRow.Item("Structure") = BDMDSSearchArrays.MaintenceFileResults(Row).StructureNo
                NewRow.Item("DocumentDate") = BDMDSSearchArrays.MaintenceFileResults(Row).DocumentDate
                NewRow.Item("Category") = BDMDSSearchArrays.MaintenceFileResults(Row).Category
                .Rows.Add(NewRow)
            Next Row

        End With

        With Adapter
            .InsertCommand = Builder.GetInsertCommand
            .UpdateCommand = Builder.GetUpdateCommand
            .Update(MyTable)
        End With

    End Sub

I adapted this code for the 3rd table, copying and pasting to make it as like as possible:

CODE

    Private Sub SaveCalcBookSearchResults()
        Dim Row As Integer = 0

        Dim strSQL As String = "SELECT CalcBook, ProjectName, Requester, Category, RequestYear, Structure, ID FROM CalcBookSearchResults"
        Dim Adapter As OleDb.OleDbDataAdapter = _
            DataLayer.GetDataAdapter(cMaintenanceManualsConnection, _
                                     strSQL)
        Dim MyTable As DataTable = _
            DataLayer.GetDataTable(Adapter, _
                                   cMaintenanceManualsConnection, _
                                   strSQL, _
                                   True)
        Dim Builder As OleDbCommandBuilder = New OleDbCommandBuilder(Adapter)

        Dim NewRow As DataRow

        With MyTable
            Adapter.DeleteCommand = Builder.GetDeleteCommand
            For Each NewRow In .Rows
                NewRow.Delete()
            Next NewRow
            Adapter.Update(MyTable)


            For Row = 0 To BDMDSSearchArrays.NumCalcBooks
                NewRow = .NewRow
                NewRow.Item("CalcBook") = BDMDSSearchArrays.CalcBookResults(Row).CalcBook
                NewRow.Item("ProjectName") = BDMDSSearchArrays.CalcBookResults(Row).ProjectName
                NewRow.Item("Requester") = BDMDSSearchArrays.CalcBookResults(Row).Requester
                NewRow.Item("Category") = BDMDSSearchArrays.CalcBookResults(Row).Category
                NewRow.Item("RequestYear") = BDMDSSearchArrays.CalcBookResults(Row).RequestYear
                NewRow.Item("Structure") = BDMDSSearchArrays.CalcBookResults(Row).StructureNo
                .Rows.Add(NewRow)
            Next Row

        End With

        With Adapter
            .InsertCommand = Builder.GetInsertCommand
            .UpdateCommand = Builder.GetUpdateCommand
            .Update(MyTable)
        End With

    End Sub

Both tables were created in the same manner (Design View) in the same Access database.  I'm scratching my head a bit here, needless to say.

RE: DataTable Delete() method inconsistent

(OP)
Oops!  The whole thing was my fault--in copying code just before the 'offending' code, I cleared the wrong internal table, so I merely added each new search's results to the old one.

As they used to say on Laugh-In, "Never mind!"

RE: DataTable Delete() method inconsistent

FYI, there is some confusion about this forum.  It got named before .Net was around, and is intended for issues surrounding database access with VB6.  If you have future issues with .Net code, you'll want to post them in the VB.Net forum.  smile

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close