By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,571 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,571 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Nov 21 '05 #2

P: n/a
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

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.