Results 1 to 2 of 2

Thread: Newbie - group looping process

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    784

    Question Newbie - group looping process

    Dear All,
    I am very new to VB.NET, I am using VB.NET 2005 and currently I am trying to do stock group looping process ..(see below)

    I need advise from the expert .. do I already have correct codes for such stock group looping process below?

    Try
    cn.Open()
    Dim cmd As New OleDbCommand("SELECT * FROM STOCKS ORDER BY STOCKGROUP,STOCKCODE", cn)
    Dim dr As OleDbDataReader = cmd.ExecuteReader()
    Dim mStockGroup as string, mStockCode as string, mStockName as string
    If dr.HasRows Then
    Do While dr.Read
    mStockGroup = dr("StockGroup")
    Do While dr.Read and dr("StockGroup") = mStockGroup ' I am not sure for this part !
    mStockCode = dr("StockCode")
    mStockName = dr("StockName")
    messagebox.show(mStockCode & " " & mStockName)
    Loop
    Loop
    End if
    dr.Close()
    Catch ex As OleDb.OleDbException
    MessageBox.show(ex.Message)
    Catch ex As Exception
    MessageBox.show(ex.Message)
    Finally
    cn.Close()
    End Try

    Thanks & regards
    Winanjaya

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Newbie - group looping process

    You're going to skip a whole bunch of rows with that code. Every time you call Read you read a row, so you can't have multiple loops.
    VB Code:
    1. mStockGroup = String.Empty
    2.  
    3. While dr.Read()
    4.     If CStr(dr("StockGroup")) <> mStockGroup
    5.         mStockGroup = CStr(dr("StockGroup"))
    6.         MessageBox.Show("New Group: " & mStockGroup)
    7.     End If
    8.  
    9.     MessageBox.Show(String.Format("Code: {0}; Name: {1}", dr("StockCode"), dr("StockName")))
    10. End While
    Also, unless you've got some other code that we don't know about you're simply overwriting the contents of mStockCode and mStockName every time, so they will simply contain the values from the last row when you're done. All the other values will be lost.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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