ASP.NET
  Home arrow ASP.NET arrow Page 2 - Bookmarking and Moving through Records wit...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Bookmarking and Moving through Records with ADO
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2006-06-07

    Table of Contents:
  • Bookmarking and Moving through Records with ADO
  • Accessing data fields and rows in a recordset
  • Moving through the Recordset and Bookmarking
  • Binding the recordset to Controls on the form

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Bookmarking and Moving through Records with ADO - Accessing data fields and rows in a recordset


    (Page 2 of 4 )

    The recordset returned by a query is the result of the query as shown in the next picture. You may access the fields by using the recordset's Fields collection and going through each of them as shown in the code. For accessing rows you may depend on the fact that the recordset opens with the current record with Absolute Position = 1 . Now you can use MoveNext to go the second record, and so on forward until you reach the last record. You will end the process when you reach the EOF. For all these positions you will collect the fields as discussed earlier.

    The fields of the recordset object

    The following code shows how you access the fields collection using an index. The picture that follows shows some of the values of the field (synonymous with a column). After you open a recordset and run this code, you will be accessing the first row. If you want to get the fields for, say,  Absolute Position=2 , just insert a rst.MoveNext after you open the recordset and before you go through the For...Next block.

    Private Sub Command9_Click()
    Dim strsql As String
    Dim cnn As New ADODB.Connection
    Dim rst As New ADODB.Recordset
    With cnn
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .Open "C:Program FilesMicrosoft OfficeOFFICE11SAMPLESNorthwind.mdb"
    End With
    strsql = "Select EmployeeID, LastName, FirstName, City from Employees"
    With rst
    Set .ActiveConnection = cnn
    .CursorType = adOpenKeyset
    .LockType = adLockOptimistic
    .Open strsql
    End With
    Debug.Print rst.Fields.Count
    strfld = ""
    For i = 0 To rst.Fields.Count - 1
    strfld = strfld + "Item.Value: " & rst.Fields.Item(i).Value & vbCrLf
    strfld = strfld + "Item.Type: " & rst.Fields.Item(i).Type & vbCrLf
    strfld = strfld + "Item.Name: " & rst.Fields.Item(i).Name & vbCrLf
    strfld = strfld + "Item.Definedsize: " & rst.Fields.Item(i).
    DefinedSize & vbCrLf strfld = strfld + "-----------------------------------" & vbCrLf Next
    Text10.SetFocus Text10.Text = strfld rst.Close cnn.Close End Sub

    The rows of the recordset

    You will implement the above code from Absolute Position = 1 up to EOF using the following code in a DO LOOP.

    Private Sub Command12_Click()
    Dim strsql As String
    Dim cnn As New ADODB.Connection
    Dim rst As New ADODB.Recordset
    With cnn
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .Open "C:Program FilesMicrosoft OfficeOFFICE11SAMPLESNorthwind.mdb"
    End With
    strsql = "Select EmployeeID, LastName, FirstName, City from Employees"
    With rst
    Set .ActiveConnection = cnn
    .CursorType = adOpenKeyset
    .LockType = adLockOptimistic
    .Open strsql
    End With
    strrows = ""
    Do While rst.EOF = False
    strrows = strrows + rst.Fields.Item(1) & "  |  " & rst.Fields.Item(2) 
    & vbCrLf strrows = strrows + "---------------------------" & vbCrLf Text10.SetFocus Text10.Text = strrows rst.MoveNext Loop
    End Sub

    In the above code, the various items of the fields collection (here only two items were collected) are accessed inside a Do Loop and at the end of the record you move the pointer to the next record using a MoveNext command. Again for display, you will gather them in a string and dump the string to a text box as shown in the following picture.

    More ASP.NET Articles
    More By Jayaram Krishnaswamy


       · Yes, there are lot more things to discuss with ADO. Even the 'Bookmark' is not...
     

    ASP.NET ARTICLES

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT