Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MoveNext/MovePrevious in .net

Status
Not open for further replies.

brews

Technical User
Dec 12, 2007
194
US
I'm trying to find a record and then move up or down in .net what I have so far is :
Code:
Public Sub Find(ByVal sLast As String, ByVal sFirst As String, _
                        ByRef bFound As Boolean, ByRef iID As Integer)
        Dim dr As DataRow
        Dim rs As  adodb.
        da = New OleDbDataAdapter()
        ds = New DataSet()
        sLast = Replace(sLast, "'", "''")
        Dim table As New DataTable
        cmd = New OleDbCommand("SELECT * FROM Members WHERE Lastname = '" & sLast & "'AND" & _
                   " Firstname = '" & sFirst & "' AND ID = " & iID, cn)
        da.SelectCommand = cmd
        da.Fill(ds, "Members")
        nMember = New CMember
        Try
            For Each dr In ds.Tables("Members").Rows
                iID = CInt(dr("ID"))
                sFirst = CStr(dr("Firstname"))
                sLast = CStr(dr("Lastname"))
            Next
            bFound = True
         Catch ex As Exception
            MessageBox.Show(ex.Message)
            bFound = False
        End Try
    End Sub
This code returns the correct record but seems like there should be a better/different way to find one (1) record than looping.

Then next part of moving up or down is puzzling to me in .net since movenext/moveprevious does not seem to exist in.net.

Any ideas are greatly appreciated. Thank you.
 
if you are not using vb.net 2008 then you could use the select method of the datatable. Els I would suggest LINQ for datatables.



Christiaan Baes
Belgium

My Blog
 
Using the code you have shown, all the data has already been loaded into table "Members" in the dataset.

What you are trying to do is move around the DataRowCollection referenced by the Rows property of the table. This is effectively a detached recordset using old ADO terminology. This collection does not support and does not need the MoveFirst, MoveNext, MovePrevious and MoveLast methods that you are used to.

The rows in this collection may be referenced by an index starting at zero. So if you wish to move to the previous row you simply subtract 1 from the current row's index and use that as the index. Add 1 to move to the next row. The first row is always index zero and the last row is always index Rows.Count - 1.

You have used the normal For Each method of iterating around the rows but you can use:
Code:
For i As Integer = 0 To ds.Tables("Members").Rows.Count - 1
as this will give you the index of the current row.

The Rows object also includes a number of useful functions that allow you to navigate around the collection. These include Contains and Find which both require that there is a Primary Key defined for the table.

A more generic method is to use the Select method of the Table itself. This allows you to specify the equivalent of a WHERE clause and returns an array of the DataRows that meet the criteria.

This array in turn may be processed by a For Each loop or a counted loop.

Note that no DB access occurs as you are simply looking at the data in RAM.


Bob Boffin
 
Thanks for the help. Seems that I need some books/tutorials particularly the manipulation of data in a db. Do you have any recommendations?

Thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top