Create datatable with (multi-field) primary key

  • Thread starter Thread starter Rob Oldfield
  • Start date Start date
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?
 
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.
 
Back
Top