Microsoft KB Archive/310372

= DataTable and DataView do not include MoveFirst, MoveLast, MoveNext, and MovePrevious navigation methods =

Article ID: 310372

Article Last Modified on 5/13/2007

-

APPLIES TO


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

-



This article was previously published under Q310372



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



SYMPTOMS
In Microsoft ActiveX Data Objects (ADO), you can use the MoveFirst, the MoveLast, the MoveNext, and the MovePrevious methods to navigate through your recordset. However, ADO.NET does not include these navigation methods.



CAUSE
Because a DataTable object is always in memory, ADO.NET does not require cursor access. ADO.NET uses array access instead.



RESOLUTION
To access a row in a DataTable, use one of the following methods:
 * Reference the row number directly.
 * If you use data binding, use the CurrencyManager class.
 * Use the For Each...Next Microsoft Visual Basic .NET statement or the foreach Microsoft Visual C# .NET statement to enumerate the rows.



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 Start Microsoft Visual Studio .NET. Create a new Windows Application project in Visual Basic .NET. Form1 is added to the project by default. Make sure that your project contains a reference to the System.Data namespace. Add a Button control to Form1. Change the Name property of the button to btnTest, and then change the Text property to Test .  Use the Imports statement on the System and the System.Data namespaces so that you are not required to qualify declarations in those namespaces later in your code. Add the following code to the &quot;General Declarations&quot; section of Form1: Imports System Imports System.Data Imports System.Data.SqlClient </li>  Add the following code in the btnTest event: Dim ds As New DataSet Dim da As SqlDataAdapter Dim cn As New SqlConnection(&quot;server=myserver;integrated security=sspi;&quot; & _                                   &quot;database=northwind&quot;) da = New SqlDataAdapter(&quot;select * from customers&quot;, cn) da.Fill(ds, &quot;customers&quot;) Dim x As Integer 'Forward For x = 1 To ds.Tables(0).Rows.Count - 1 Console.WriteLine(ds.Tables(0).Rows(x).Item(&quot;customerid&quot;).ToString) Next 'Backward For x = ds.Tables(0).Rows.Count - 1 To 1 Step -1 Console.WriteLine((ds.Tables(0).Rows(x).Item(&quot;customerid&quot;).ToString)) Next </li> Modify the connection string as appropriate for your environment.</li> Save and run the project.</li> Click Test. Notice the output in the output window.</li></ol>

<div class="references_section">