How do I delete the result of a Select statement from the original datatable?

B

Bmack500

Hello, and thanks in advance.

I've got a bit of code below, which just iterates through a string
array (WSVIP), uses a select statement to fetch an array of datacolums;
I would then like to iterate through each found row and delete it from
the original database.

I can't use a datatable.find method, since MACH_NAME is not a unique
field and I cannot index on it.

Am I going to have to iterate each row in the datatable and compare the
relevent columns, or is there an easier way to accomplish this?

Any help would be greatly appreciated. Thanks!

For Each strItem In wsVIP
If Not InStr(strItem, "*") > 0 Then
strCalc = "MACH_NAME = '" & strItem & "'"
Else
strCalc = "MACH_NAME LIKE '" & strItem & "'"
End If
foundRows = dsIava.Tables(0).Select(strCalc)
recCount = foundRows.Length
If Not recCount = 0 Then
For i = 0 To recCount

'HOW DO I DELETE THE FOUND ROWS FROM THE ORIGINAL
DATATABLE?

Next
End If
Next
dsIava.Tables(0).AcceptChanges()
 
W

W.G. Ryan [MVP]

If I understand you correctly , you should just be able to
dsIava.Tables(0).Rows(i).Delete - Note you can't do this in a foreach loop
b/c it changes the collection, but you can with an index based loop. Those
rows will still be there, albeit with rows marked as deleted until the loop
exits and you call AcceptChanges - but after that they'll be gone.

HTH,

Bill
 
B

Bmack500

Sure, but how I get the datarow index (of the datatable) out of the
foundrows? It seems to me that the select statement creates it's own
index, and doesn't retain the identity of the original row in the
datatable.
If I'm wrong, please correct me but when I tried it that way, despite
having many hits the row count in the datatable was the same after
calling acceptchanges.
 
B

Bmack500

Sorry, let me clarify - it deletes a row in the datatable, but not the
right one because I don't know the datatable row index based off of the
found rows.
 
B

Bmack500

Okay, I've solved it like this - not very elegant, but it works!! I can
just use the keys I do have. Do you see any potential pitfalls in doing
it like this?

PS - I never need to update the original datasource.

For Each strItem In wsVIP
If Not InStr(strItem, "*") > 0 Then
strCalc = "MACH_NAME = '" & strItem & "'"
Else
strCalc = "MACH_NAME LIKE '" & strItem & "'"
End If

strKey(0) = strItem
foundRows = dsIava.Tables(0).Select(strCalc)

recCount = foundRows.Length
If Not recCount = 0 Then
For Each dr In foundRows
strKey(0) = dr.Item(0).ToString
strKey(1) = dr.Item(2).ToString
strKey(2) = dr.Item(1).ToString
arrRow = dsIava.Tables(0).Rows.Find(strKey)
arrRow.Delete()
Next
End If
Next
dsIava.Tables(0).AcceptChanges()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top