• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 431
  • Last Modified:

How do I load data into List (Of T)

I am writing a new application and can't remember how to load data from a database into List(Of T).  MS Access was the best choice for this customer, so I'm using OleDB.  I have a class called Store that has the properties for the Store table's data.

How do I populate the List(Of T)?
Here's my code so far:

 
Private Sub LoadStores()
        Dim colStores As New List(Of Store)
        objConnection = objEngine.GetConnection
        Dim sql As String = "SELECT StoreID,StoreAbbreviation,StoreState,StoreName "
        sql &= "FROM Stores ORDER BY StoreName;"
        Dim cmd As New OleDb.OleDbCommand
        cmd.CommandText = sql
        cmd.ExecuteNonQuery()

    End Sub

Open in new window

0
Agent909
Asked:
Agent909
1 Solution
 
Agent909Author Commented:
I figured it out.  The code isn't 'pretty' yet, but it works.


Private Sub LoadStores()
        Dim s As New Store
        Dim r As OleDb.OleDbDataReader
        Dim colStores As New List(Of Store)
        objConnection = objEngine.GetConnection
        Dim sql As String = "SELECT StoreID,StoreAbbreviation,StoreState,StoreName "
        sql &= "FROM Stores ORDER BY StoreName;"
        Dim cmd As New OleDb.OleDbCommand
        cmd.CommandText = sql
        cmd.Connection = objConnection
        objConnection.Open()
        r = cmd.ExecuteReader
        If r.HasRows Then
            While r.Read()
                s = New Store
                s.StoreID = r("StoreID")
                s.StoreAbbreviation = r("StoreAbbreviation")
                s.StoreState = r("StoreState")
                s.StoreName = r("StoreName")
                colStores.Add(s)
            End While
            gridStores.DataSource = colStores
        Else
            MessageBox.Show("There are NO stores entered.")
        End If
        objConnection.Close()
    End Sub

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now