Results 1 to 13 of 13

Thread: Looping process with datareader

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    784

    Question Looping process with datareader

    Dear The Expert,
    I don't understand .. why my looping process below (with level break condition) does not working ..I meant the "mTotalFreq" always has incorrect value.. it seems like the looping process has skipped one or some records, am I missing something?

    DataReader = Cmd.ExecuteReader
    While DataReader.Read
    mStat = DataReader("Stat")
    mHostCode = DataReader("HostCode")
    While DataReader.Read = True And DataReader"HostCode") = mHostCode
    mTotalFreq = mTotalFreq + DataReader("TotalCost")
    End While
    End While

    DataReader.Close()
    SQLTemp.Close()

    I need advise .. many thanks in advance

    Regards
    Winanjaya

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Looping process with datareader

    Originally posted by Winanjaya
    Dear The Expert,
    I don't understand .. why my looping process below (with level break condition) does not working ..I meant the "mTotalFreq" always has incorrect value.. it seems like the looping process has skipped one or some records, am I missing something?

    DataReader = Cmd.ExecuteReader
    While DataReader.Read
    mStat = DataReader("Stat")
    mHostCode = DataReader("HostCode")
    While DataReader.Read = True And DataReader"HostCode") = mHostCode
    mTotalFreq = mTotalFreq + DataReader("TotalCost")
    End While
    End While

    DataReader.Close()
    SQLTemp.Close()

    I need advise .. many thanks in advance

    Regards
    Winanjaya
    Yes you're missing something. Everytime you do the DataReader.Read, it moves to the next record. So you are getting an incorrect value.

  3. #3
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    In other words, using nested Reads of the same reader results in more than one record getting used during a process.
    Whadayamean it doesn't work....
    It works fine on my machine!

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    And use .HasRows to check if there are any rows in the datareader to be read...

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    784
    Could any body please give me a simple code example about this? .. sorry I am very new in Vb.NET?

    thanks a lot in advance

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    VB Code:
    1. Dim dr As System.Data.SqlClient.SqlDataReader
    2. dr = SqlCommand1.ExecuteReader(CommandBehavior.CloseConnection)
    3.  
    4. If dr.HasRows Then
    5.  While dr.Read
    6.       'this code will run once for each record returned
    7.       If dr("HostCode") Then mTotalFreq = mTotalFreq + dr("TotalCost")
    8.       'alternative statement if you know which column values are
    9.       'this runs faster
    10.       If dr(1) Then mTotalFreq = mTotalFreq + dr(2)
    11.      
    12.  End While
    13. End If
    14.  
    15. dr.Close()

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    784
    Thanks, but how to group it?

    Dim dr As System.Data.SqlClient.SqlDataReader
    dr = SqlCommand1.ExecuteReader(CommandBehavior.CloseConnection)

    If dr.HasRows Then
    While dr.Read
    mhostcode=dr("hostcode")
    mtotalfreq=0
    while dr.read and mhostcode=dr("hostcode")
    mtotalfreq=mtotalfreq+dr("totalcost")
    end while
    ' I STILL GET THE INCORRECT MTOTALFREQ here ..
    SOME RECORDS SKIPPED HERE !
    End While
    End If

    dr.Close()

  8. #8
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Explain exactly what your goal is, in layman terms...



    VB Code:
    1. mhostcode=dr("hostcode")
    2. mtotalfreq=0
    3. 'this following line does not make sense
    4. 'you cannot get mhostcode from the
    5. 'datareader unless you do a dr.read
    6. 'to begin with
    7. while dr.read and mhostcode=dr("hostcode")

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    784
    I want to get totalfreq of each hostcode

    while dr.read
    mhostcode=dr("hostcode") ' Store hostcode to variable
    mtotalfreq=0 ' Set 0 to mtotalfreq


    while dr.read and mhostcode=dr("hostcode")
    ' sum the totalcost to mtotalfreq
    mtotalfreq=mtotalfreq+dr("totalcost")
    end while

    msgbox mtotalfreq

    end while

    any idea?

  10. #10
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    You cannot use a While dr.Read and mhostcode=...

    Otherwise, the reader will never advance to the next record.

    Additionally, you can only have one datareader on an open connection at one time.

    What format is the record coming as? By that I mean what columns are in the row (record)?

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    784
    So what should I use? is there another way for doing this? I meant by do that looping process I will get the value of mtotalfreq of each hostcode.. I need advise ...thanks

  12. #12
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    What about changing the query of the DataReader. Makes your loop much simpler and the database creates the totals.
    VB Code:
    1. Select HostCode, Sum(TotalCost)
    2. From
    3. Group By HostCode
    4.  
    5. While dr.Read
    6. 'do something
    7. End While

  13. #13
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    You need to state how the data is being presented to you.

    For instance, if a SQLPROC named 'GetHostElements' looks like this:
    Code:
    Proc GetHostElements
    
    Select mHostCode, mtotalfreq From HostElements
    Then the returning data will have two columns, mHostCode, mTotalFreq.

    Our code to read that would be:
    VB Code:
    1. While dr.Read()
    2.    somevariable = dr(0)
    3.    someothervariable = dr(1)
    4. End While

    Now obviously, if we have more than 1 row being returned from the stored procedure... then we need to put those values somewhere other than assigning them to the same variables (somevariable and someothervariable). Because on each loop, those two variables will get reassigned the contents of the datareader at that particular record.

    So you either need an array or a datatable to store these values. If you use a datatable:

    we adjust our code to this:
    VB Code:
    1. Dim dt As New DataTable
    2. dt.Columns.Add(New DataColumn("mHostCode", GetType(String)))
    3. dt.Columns.Add(New DataColumn("mTotalFreq", GetType(Integer)))
    4.  
    5. ...do your sql connection and command decleration...
    6.  
    7. dr = SqlCommand1.ExecuteReader(CommandBehavior.CloseConnection)
    8. Dim datar As DataRow
    9. While dr.Read
    10.   datar = dt.NewRow
    11.   datar(0) = dr(0)
    12.   datar(1) = dr(1)
    13.   dt.Rows.Add(datar)
    14. End While
    15. dr.Close
    16.  
    17. 'we now have a table reflecting all the records returned from our proc

    Now I used this as an example, because I don't have any idea what your database is supposed to return, because you didn't state that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width