Create datatable with (multi-field) primary key

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?
 
H

Herfried K. Wagner [MVP]

Rob,

* "Rob Oldfield said:
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?

You are posting to the wrong group. This group is about .NET Windows
Forms programming, your question is not related to Windows Forms. I
would suggest to post to the
<group.
 

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