DataRowCollection::Find problem

  • Thread starter Thread starter Tom Archer
  • Start date Start date
T

Tom Archer

In using the DataRowCollection::Find with the
NorthWind/Employees table, I'm getting an exception
stating that the table doesn't contain a primary key -
which it does (EmployeeID). Has anyone else run into
problems with this method?
 
Tom Archer said:
In using the DataRowCollection::Find with the
NorthWind/Employees table, I'm getting an exception
stating that the table doesn't contain a primary key -
which it does (EmployeeID). Has anyone else run into
problems with this method?

Tom,
In order to use the Find method, the DataTable object to which the
DataRowCollection object belongs to must have at least one column
designated as a primary key column.
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemdatadatatableclassprimarykeytopic.asp

See the PrimaryKey property for details on creating a PrimaryKey column,
or an array of DataColumn objects when the table has more than one primary key.
http://msdn.microsoft.com/library/e...ystemdatadatarowcollectionclassfindtopic2.asp

--

Thanks,
Carl Prothman
Microsoft ASP.NET MVP
http://www.able-consulting.com
 
Thanks Carl.

Just to clarify that I'm understanding this correctly:

ADO.NET can't figure out that the underlying table has a
primary key so I must explicitly tell it which column is
the primary key?
 
Tom Archer said:
Just to clarify that I'm understanding this correctly:

ADO.NET can't figure out that the underlying table has a
primary key so I must explicitly tell it which column is
the primary key?

Tom,
For an untyped DataSet, the primary key information is NOT filled in.
In this case, you'll need to specify which column(s) is the primary key.

However, for a Typed DataSet, then the primary key information
is filled in for you.

e.g.

// Untyped DataSet
DataSet dataSet = new DataSet();
sqlDataAdapter1.Fill(dataSet, "Employees");
int count1 = dataSet.Tables["Employees"].PrimaryKey.Length;
// count1 = 0

// Typed DataSet
EmployeesDataSet employeeDataSet = new EmployeesDataSet();
sqlDataAdapter1.Fill(employeeDataSet.Employees);
int count2 = employeeDataSet.Employees.PrimaryKey.Length;
// count2 = 1

--

Thanks,
Carl Prothman
Microsoft ASP.NET MVP
http://www.able-consulting.com
 
Carl Prothman said:
For an untyped DataSet, the primary key information is NOT filled in.
In this case, you'll need to specify which column(s) is the primary key.

Or set the MissingSchemaAction before the Fill method:
sqlDataAdapter1.MissingSchemaAction = MissingSchemaAction.AddWithKey

--

Thanks,
Carl Prothman
Microsoft ASP.NET MVP
http://www.able-consulting.com
 
Back
Top