Solved

How do I load data into List (Of T)

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 40
Java string replace 11 45
How to determine if a string is a valid SHA value 7 31
printf performancy 11 37
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

770 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