Solved

How do I load data into List (Of T)

Posted on 2011-02-25
1
420 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now