Solved

How do I load data into List (Of T)

Posted on 2011-02-25
1
423 Views
Last Modified: 2012-05-11
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
Comment
Question by:Agent909
1 Comment
 
LVL 1

Accepted Solution

by:
Agent909 earned 0 total points
ID: 34985944
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question