Re: why it always reads from second record?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Re: why it always reads from second record?

  1. #1
    Phil Weber Guest

    Re: why it always reads from second record?

    > Why the following loop always reads from the
    > second record?


    Holly: The .Read method reads the next record; you're calling it twice:

    ' Reads first record
    If db.dbReader.Read() = True Then
    ' Reads second record
    Do While db.dbReader.Read

    Simply eliminate the outer If...Then, and you'll see all the records.
    --
    Phil Weber


  2. #2
    holly Guest

    Re: why it always reads from second record?


    Thanks, Phil. But i want to use 'If...then' to check whether there are any
    records before i do loop.
    Holly

    "Phil Weber" <philweber@hotmail.com> wrote:
    > > Why the following loop always reads from the
    > > second record?

    >
    >Holly: The .Read method reads the next record; you're calling it twice:
    >
    > ' Reads first record
    > If db.dbReader.Read() = True Then
    > ' Reads second record
    > Do While db.dbReader.Read
    >
    >Simply eliminate the outer If...Then, and you'll see all the records.
    >--
    >Phil Weber
    >



  3. #3
    Phil Weber Guest

    Re: why it always reads from second record?

    > Thanks, Phil. But I want to use 'If...Then' to check
    > whether there are any records before I do loop.


    Holly: Why? The loop won't do anything if there are no records.

    There's no way in .NET 1.0 to check whether a DataReader contains data
    (version 1.1 adds a .HasRows method), and a DataReader is forward-only, so
    there's no way to reset the record pointer to the beginning of the
    DataReader after you've started reading.

    You could use a DataSet instead of a DataReader; that would allow you to
    determine the row count before your loop. Or you could do a separate query
    (SELECT COUNT(*)...) to determine the number of rows that will be returned
    before you actually query for the data.
    --
    Phil Weber


  4. #4
    holly Guest

    Re: why it always reads from second record?


    Thanks,Phil

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center