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
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:
mStockGroup = String.Empty
While dr.Read()
If CStr(dr("StockGroup")) <> mStockGroup
mStockGroup = CStr(dr("StockGroup"))
MessageBox.Show("New Group: " & mStockGroup)
End If
MessageBox.Show(String.Format("Code: {0}; Name: {1}", dr("StockCode"), dr("StockName")))
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.