I need a fresh pair of eyes. (DataTable.Select)

Lotok

Well-known member
Joined
Jan 17, 2012
Messages
198
Location
Scotland
Programming Experience
5-10
I've done these a million times but for some reason this one isn't working, I just cant see why.


Dataset mapds has 1 table and 1 column with 40k rows of numbers, int64's mainly but some smaller.
All I am doing is comparing a set of numbers in another dataset to the numbers in mapds. If they match then delete the row.

the variable 'row' points to the other data set. ds.tables(0).rows(i)

When stepping into the code I can see that CStr(row.Item(4)).Remove(".0") & "'") is returning the correct number and that the column of the dataset I am searching shows MPRN. But my found() (datarow array) never gets any values. So for some reason the select isn't working.
Just after some fresh ideas and ideas. No doubt some stupid typo.


Dim tmptable As DataTable = mapds.Tables(0)
Dim found() As DataRow = Nothing
Dim dr As DataRow
found = tmptable.Select("MPRN = '" & CStr(row.Item(4)).Remove(".0") & "'")
'check for match and delete
For Each dr In found
dr.Delete()
Next

[/code]
 
For the moment I will just do it the long way. I cant see why the select doesnt work, this does but more intensive.


For p = 0 To tmptable.Rows.Count - 1
If tmptable.Rows(p).Item(0) = CType(row.ItemArray(4), Int64) Then
row.Delete()
Exit For
End If
Next
 
If you are going to delete you must add DataTable.AcceptChanges() after your for next statement

For Each row As DataRow in Found
dr.Delete()
Next
dr.AcceptChanges
 
I don't understand your code

String.Remove takes a NUMBER parameter and is the index to start removing characters at. Essentially it is like a LEFT() function
"abcdef".Remove(3) 'returns "abc"

You are passing a string to remove, and I've no idea why that even works: there is no overload of Remove() that accepts string. Are you making the crazy mistake of programming with Option Strict/Option Explicit set of OFF? Never should do that; it's one of the biggest causes of sloppy VB programming and one of the main reasons why VB is looked down upon as a "noddy language"

Anyways, suppose VB is converting your ".0" to 0, Remove(0) would return an empty string. Your datatable select clause would be "MPRN= '' " and no rows have a MPRN that is empty (or there is a confusion over whether an empty string is equal to null)

If by Remove(".0") you mean Replace(".0", "") then you need the latter, not the former

But check that Option Strict/Explicit are ON ...
 
Note that the two code samples you posted are not equivalent; the second sample converts Item 4 to an int, the first to a string
 
Back
Top