DataTable and DataView do not include MoveFirst, MoveLast, MoveNext, and MovePrevious navigation methods (310372)



The information in this article applies to:

  • Microsoft ADO.NET (included with the .NET Framework) 1.0
  • Microsoft ADO.NET (included with the .NET Framework 1.1)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual Basic .NET (2003)

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.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Microsoft Visual Studio .NET.
  2. Create a new Windows Application project in Visual Basic .NET. Form1 is added to the project by default.
  3. Make sure that your project contains a reference to the System.Data namespace.
  4. Add a Button control to Form1. Change the Name property of the button to btnTest, and then change the Text property to Test.
  5. 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 "General Declarations" section of Form1:
          Imports System
          Imports System.Data
          Imports System.Data.SqlClient
    					
  6. Add the following code in the btnTest event:
            Dim ds As New DataSet()
            Dim da As SqlDataAdapter
            Dim cn As New SqlConnection("server=myserver;integrated security=sspi;" & _
                                        "database=northwind")
            da = New SqlDataAdapter("select * from customers", cn)
            da.Fill(ds, "customers")
            Dim x As Integer
            'Forward
            For x = 1 To ds.Tables(0).Rows.Count - 1
                Console.WriteLine(ds.Tables(0).Rows(x).Item("customerid").ToString)
            Next
            'Backward
            For x = ds.Tables(0).Rows.Count - 1 To 1 Step -1
                Console.WriteLine((ds.Tables(0).Rows(x).Item("customerid").ToString))
            Next
    					
  7. Modify the connection string as appropriate for your environment.
  8. Save and run the project.
  9. Click Test. Notice the output in the output window.

REFERENCES

For more information about the CurrencyManager class, refer to the following MSDN Web site:

Modification Type:MinorLast Reviewed:3/9/2006
Keywords:kbprb kbSqlClient kbSystemData KB310372 kbAudDeveloper