Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataTable Delete() method inconsistent

Status
Not open for further replies.

DENewkirk

Programmer
Jan 19, 2006
14
US
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
            [b]Adapter.DeleteCommand = Builder.GetDeleteCommand
            For Each NewRow In .Rows
                NewRow.Delete()
            Next NewRow
            Adapter.Update(MyTable)[/b]

            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
            [b]Adapter.DeleteCommand = Builder.GetDeleteCommand
            For Each NewRow In .Rows
                NewRow.Delete()
            Next NewRow
            Adapter.Update(MyTable)[/b]

            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.
 
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!
 
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. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top