How to find a row in datatable by passing named arguments for PK?

B

Billy

I need some help with solving my problem.

I'm writing general procedure to find a values in any table. My idea is
to pass to that procedure: TableName and ArrayList with parametere.
Arraylist contain FieldName and Value for that FieldName. Every table
has Primary key which can have one or more fields in that PK.

My problems is that I don't know how to pass that parameters in
arbitrary way. For e.g. in below example from help you have to pass
parameters in EXACT same order as fields in PK is written. So how to
write below example that each passed value will be bound to specific
field in PK not with index but by name of the field in PK??

Private Sub FindInMultiPKey(ByVal myTable As DataTable)
Dim foundRow As DataRow
' Create an array for the key values to find.
Dim findTheseVals(2) As Object
' Set the values of the keys to find.
findTheseVals(0) = "John"
findTheseVals(1) = "Smith"
findTheseVals(2) = "5 Main St."
foundRow = myTable.Rows.Find(findTheseVals)
' Display column 1 of the found row.
If Not (foundRow Is Nothing) Then
Console.WriteLine(foundRow(1).ToString())
End If
End Sub
 
C

Cor Ligthert [MVP]

Billy,

What is the sense of an arraylist if you can do it like this

\\\
Dim FindTheseValues() As Object = {"John", "Smit", "5 Main St."}
FindInMultiPKey(myTable,FindTheseValues)

Private Sub FindInMultiPKey(ByVal myTable As DataTable, ByVal FindTheseVals
as Object())
Dim foundRow As DataRow
foundRow = myTable.Rows.Find(findTheseVals)
' Display column 1 of the found row.
If Not (foundRow Is Nothing) Then
Console.WriteLine(foundRow(1).ToString())
End If
End Sub
///

I hope this helps,

Cor
 
B

Billy

Cor, thanks for your replay.

I know that I can also pass parameters only with Array. But I would
like to pass that parameters in arbitrary way. And for that I need name
for each field in PK. And that way code is also more clear so that I
know exactly what I pass to the procedure. Do you know maybe how to
display a list (with some loop) of names in PK? If I would know that,
then I can sort my input list in order regarding to that list and
eventually could solve my problem either. Anyway at the worst case it
can stay like you propose.

Regards,
Billy
 

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