Over a million developers have joined DZone.

Using Rows.Find In ADO.NET

When using a DataTable in ADO.NET, you may want to search your table given a key. The Find method requires the index of the primary key to find the row that you're looking for. However, before you do this you must set the primary key of the Datatable. Setting the primary key in your database is not enough. To set the primary key, create a new column and then set the primary key field on the Datatable like so:

// Create the DataColumn list for holding the columns of the primary keys. In this case, we are using only one primary key
DataColumn[] primaryKeyColumns = new DataColumn[1];

// Set the first primary key to the column 'id' in the datatable
primaryKeyColumns[0] = myDataTable.Columns["id"];

// Now apply the primary key to the datatable
myDataTable.PrimaryKey = primaryKeyColumns;

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}