Microsoft KB Archive/305561

= HOW TO: Locate a Specified DataRow by Using the Find Method of DataRowCollection in Visual Basic .NET =

Article ID: 305561

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft ADO.NET 1.1
 * Microsoft ADO.NET 1.0
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q305561





This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Data

IN THIS TASK

 * SUMMARY
 * Additional Information About the Find Method
 * Sample Code
 * Troubleshoot
 * REFERENCES



SUMMARY
This step-by-step article describes how to locate a specified DataRow object by using the Find method of the DataRowCollection.

back to the top

Additional Information About the Find Method
The 'Find' method queries the primary key column of the DataTable, and locates the record that exactly matches the value that is passed in the search criteria. If the DataTable does not have a primary key, a MissingPrimaryKeyException is raised. The Find method has the following limitations:  The Find method is not case sensitive.  You cannot use Wildcard characters in the Find method. For example, when you use the following code, you may not find all the rows starting with Ch. Instead, you receive the row with the primary key value Ch%. myDataRow = myDataTable.Rows.Find(&quot;Ch%&quot;) 

The Find method returns a DataRow that matches the search criteria. When no match is found, an empty DataRow object is returned. Using Is Nothing, you can detect the results of the Find method.

back to the top

Sample Code
 Create a new Visual Basic .NET Console Application project. By default, Module1.vb is created.  Replace the existing code in Module1.vb with the following code: Imports System.Data Imports System.Data.SqlClient

Module Module1

Sub Main ' Open connection. Dim cn As SqlConnection cn = New SqlConnection(&quot;User ID=sa;Initial Catalog=pubs;Data Source=mySQLServerName&quot;)

' Create DataAdaptor. Dim da As SqlDataAdapter da = New SqlDataAdapter(&quot;Select * from Employee&quot;, cn)

' Create a Dataset. Dim ds As DataSet ds = New DataSet

' Get Schema such as primary key from the database. da.FillSchema(ds, SchemaType.Mapped, &quot;Employee&quot;)

'Get the data from database. da.Fill(ds, &quot;Employee&quot;)

' DataRow to collect result of Find method. Dim dr As DataRow ' Find Employee with specified ID. Console.WriteLine(&quot;Searching for Employee ID = KFJ64308F&quot;) dr = ds.Tables(&quot;Employee&quot;).Rows.Find(&quot;KFJ64308F&quot;) ' Check whether the search returned any result. If Not (dr Is Nothing) Then Console.WriteLine(&quot;First Name is : {0}&quot;, dr(&quot;fname&quot;)) Else Console.WriteLine(&quot;No Matching Rows Found.&quot;) End If

' Try to use wildcard character. Console.WriteLine(&quot;Searching for all Employees with Employee ID starting with K (Wildcard K%)&quot;) dr = ds.Tables(&quot;Employee&quot;).Rows.Find(&quot;K%&quot;) ' Check whether the search returned any result. If Not (dr Is Nothing) Then Console.WriteLine(&quot;First Name is : {0}&quot;, dr(&quot;fname&quot;)) Else Console.WriteLine(&quot;No Matching Rows found with specified search criteria.&quot;) End If     Console.ReadLine End Sub

End Module  Modify the connection string as appropriate for your environment.</li> On the Debug menu, click Start to run the application.</li></ol>

back to the top

Troubleshoot
The MissingPrimaryKeyException exception is thrown when you try to access a row in a table that does not have a primary key column.

For more information about MissingPrimaryKeyException, see the following Microsoft Developer Network (MSDN) Web site:

MissingPrimaryKeyException Class

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataMissingPrimaryKeyExceptionClassTopic.asp

back to the top

<div class="references_section">