For Each Loop with a dataset

  • Thread starter Thread starter Nitromuse
  • Start date Start date
N

Nitromuse

What is the proper way to refer to a dataset as the
collection in a For Each, Next Statement? I want to loop
through a particular column in the dataset, I've tried
the following with no sucess.

For Each x as integer in Dataset1
....
....
....
Next x

But the dataset name is not a valid collection type.
I appreciate your help, Seasons Greetings
 
Nitromuse said:
What is the proper way to refer to a dataset as the
collection in a For Each, Next Statement? I want to loop
through a particular column in the dataset, I've tried
the following with no sucess.

For Each x as integer in Dataset1
....
....
....
Next x

But the dataset name is not a valid collection type.
I appreciate your help, Seasons Greetings

dim row as datarow

for each row in dataset1.tables("tablename").rows
debug.writeline row("columnname")
next

I assume Dataset1 is the name of the variable referencing the Dataset
object.
 
Thanks for the help Armin, I'm using the following and
getting a 'Object reference not set to an instance of an
object' at the For Each line, what am I missing.

Private Sub Button10_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button10.Click
Dim row As DataRow
Dim ds As DataSet
ds = New System.Data.DataSet("Payroll.DataSet1")
For Each row In ds.Tables("Payroll.DataSet1").Rows
Debug.WriteLine(row("columnname"))
Next
End Sub
Thanks again,
 
Nitromuse said:
Thanks for the help Armin, I'm using the following and
getting a 'Object reference not set to an instance of an
object' at the For Each line, what am I missing.

Private Sub Button10_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button10.Click
Dim row As DataRow
Dim ds As DataSet
ds = New System.Data.DataSet("Payroll.DataSet1")
For Each row In ds.Tables("Payroll.DataSet1").Rows
Debug.WriteLine(row("columnname"))
Next
End Sub
Thanks again,


You do not add any tables to the Dataset. You also don't fill the Dataset
with records. If the Dataset is completely empty, using For-Next doesn't
make sense.

How to use datasets:
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcreatingusingdatasets.asp
http://msdn.microsoft.com/library/en-us/vbcon/html/vboriDatasets.asp
 
Once again, thank you! I've filled the dataset with a
dataAdapter and fill command before running the sub I
referenced, (which I didn't include in the previous
message). I think I'm just missing how to re-instanciate
the object properly.
Again, THANX
 
Hi Nit,

Here's a very basic example of how to fill a dataset with data before
looping through it:
Dim oconn As New SqlConnection("data source=d5z0071;database=imc;integrated
security=sspi;")

Dim dabnlsum As New SqlDataAdapter("select distinct invnum from bnlsum order
by invnum", oconn)

Dim dsbnlsum As New DataSet("bnlsum")

oconn.Open()

dabnlsum.Fill(dsbnlsum, "bnlsum")

Dim irow As DataRow

For Each irow In dsbnlsum.Tables(0).Rows

etc

HTH,

Bernie Yaeger
 
Hi,

Dim conn As OleDbConnection

Dim strConn As String

Dim strSQL As String

Dim da As OleDbDataAdapter

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

strConn += "Data Source = Northwind.mdb;"

conn = New OleDbConnection(strConn)

da = New OleDbDataAdapter("Select * From Categories", conn)

da.Fill(ds, "Categories")

For Each dr As DataRow In ds.Tables("Categories").Rows

Debug.WriteLine(dr.Item("CategoryName"))

Next

Ken

----------------------
 
Thanks Ken, thanks Armin
It was simply the line;

For Each dr As DataRow In DataSet11.Tables("Comp").Rows

that I didn't understand as how to refer to the row in a
dataset. Thanks again,
 
Back
Top