deleting rows from a datatable

alexlashford

Member
Joined
Jul 20, 2007
Messages
7
Programming Experience
Beginner
hello

I have two datatables one called ScheduledOrdersTable and the other called AllOrdersTable which both have within them a field called OrderNO.

AllOrdersTable is the datasource for a datagrid on one of my forms.

what i want to do is remove/delete any row that occurs in the ScheduledOrdersTable from the AllOrdersTable, so that a row with a specific orderNo cannot occur in both tables.

this code returns a message saying there is no row at position 89

If ScheduledOrdersTable.Rows.Count > 0 Then
Dim d As Integer
For d = 0 To ScheduledOrdersTable.Rows.Count() - 1

Dim orderNo As String =ScheduledOrdersTable.Rows(d).Item("OrderNo")

Dim x As Integer
For x = 0 To AllOrdersTable.Rows.Count - 1
If AllOrdersTable.Rows(x).Item("ORDER_NUMBER") = orderNo Then
AllOrdersTable.Rows(x).Delete()

End If
Next
Next
End If

#
but if i change the line in red to:

For x = 0 To AllOrdersTable.Rows.Count - 2

all the rows that should be removed are except for the last row in the table

what i want to achive is surley quite simple can anybody help,

thanks in advance

alex :)
 
Problem solved!

i did try that and it still didn't work!

so i just re-wrote the code, thought out the logic a bit more and found the error was with my FOR loop

I did use the remove command insted of the delete

can anyone tell me what the difference is

thanks again

:)
 
calling .Delete() marks the row for deletion as Arg says. The next time that AcceptChanges() is called on the datatable, the row is removed from it. AcceptChanges is called implicitly during a tableadapter .Update

You are recommended to use relations to cascade deletes within a dataset. Within the database you MUST engage your brain in a design decision first.

If the database is cascading deletes then you DONT send a child datatable with deleted rows into a table adapter after you Update() the parent, because the deleted rows dont exist any more in the database and you get an error

Child datatables should generally be Update()ed first. Regardless of whether a database is cascading deletes, updating the child first is a safe way to complete the operation
 
Back
Top