Create datatable with (multi field) PK

R

Rob Oldfield

I'm trying to create a datatable with a primary key based on two datacolumns
(in fact, I'm getting the same error even if I just try a single column
key). I'm setting the table up with the code below (which appears to work
correctly), but when I then try to use the Find method on the table then I'm
getting told that it's failed because there's no primary key.

dc = New DataColumn()
dc.DataType = System.Type.GetType("System.Decimal")
dc.ColumnName = "Value"
dtResult.Columns.Add(dc)

dc = New DataColumn()
dc.DataType = System.Type.GetType("System.String")
dc.ColumnName = "Code"
dtResult.Columns.Add(dc)

dc = New DataColumn()
dc.DataType = System.Type.GetType("System.String")
dc.ColumnName = "Date"
dtResult.Columns.Add(dc)

Dim PrimaryKeyColumns(1) As DataColumn
PrimaryKeyColumns(0) = dtResult.Columns("Code")
PrimaryKeyColumns(1) = dtResult.Columns("Date")
dtResult.PrimaryKey = PrimaryKeyColumns

Anyone have an idea where I'm going wrong?
 
C

Cor

Hi Rob,

I did not do it a while that way and now I am in doubt, but I thougth you
had to add that primarykey column to your table
Dim PrimaryKeyColumns(1) As DataColumn
PrimaryKeyColumns(0) = dtResult.Columns("Code")
PrimaryKeyColumns(1) = dtResult.Columns("Date")
dtResult.PrimaryKey = PrimaryKeyColumns
dtResult.columns.add(PrimaryKeyColumns)

I hope this helps?

Cor
 
R

Rob Oldfield

Thanks for the idea, but it doesn't work. Comes up saying that it can't
call the Add method with a one-dimensional array as the argument.
 
C

Cor

Hi Rob,

Sorry, no it was not, I have now looked in a program part where I use it.
I have changed a lot to make it not to complex (originaly it goes with 2
datasets) so do not look at typos

Maybe you can use ist as a template to check

I hope this is better information?

Cor
\\\\
Dim findKey(1) As Object
Dim dsKey(1) As DataColumn
dsKey(0) = Tables0.Columns("x")
dsKey(1) = Tables0.Columns("y")
Tables0.PrimaryKey = dsKey
Dim dr As DataRow
For Each dr In Tables0
findKey(0) = x
findKey(1) = y
dr = Tables0.Rows.Find(findKey)
next
///
 
R

Rob Oldfield

Oops. I was being a cretin. When I was running the Find command I was
using the wrong table - which doesn't have a PK. Thanks for helping anyway.
 

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