We help IT Professionals succeed at work.

Dataset to Collection of objects

Rowan Scott
Rowan Scott asked
on
Hi

I'm using Visual Basic Express 2010 and can successfully get a dataset from an access database. How do I navigate through the dataset to create a collection of custom objects.

See attached code.
Dim conn As String = My.Settings.ConnectionString
Dim cmd As String = "SELECT Employees.* FROM Employees;"

Dim adapter As New OleDbDataAdapter(cmd, conn)
Dim topics As New DataSet
        adapter.Fill(topics)


        'Users is a public declared collection
        Users = New Collection
        Dim Person As c_Person

        'This is where I am stuck
        Dim i As Long
        For i = 0 To topics.Tables(0).Rows.Count
            Person = New c_Person
            Person.FullName=?
            Person.Password=?
            Users.Add(Person)
        Next

Open in new window

Comment
Watch Question

Commented:
try this,
change "FullName" and "Password" to the actual field names


Dim conn As String = My.Settings.ConnectionString
Dim cmd As String = "SELECT Employees.* FROM Employees;"

Dim adapter As New OleDbDataAdapter(cmd, conn)
Dim topics As New DataSet
        adapter.Fill(topics)


        'Users is a public declared collection
        Users = New Collection
        Dim Person As c_Person

        'This is where I am stuck
        Dim i As Long
        For i = 0 To topics.Tables(0).Rows.Count
            Person = New c_Person
            Person.FullName=topics.Tables(0).Rows(i).item("FullName")
            Person.Password=topics.Tables(0).Rows(i).item("Password")
            Users.Add(Person)
        Next

Open in new window

Commented:
also change
For i = 0 To topics.Tables(0).Rows.Count
to
For i = 0 To topics.Tables(0).Rows.Count -1

Author

Commented:
Thanks