How to fill listbox from SQL query

Posted on 2005-05-05
Last Modified: 2007-11-27
I'm writing a small VB.NET app.
How would I fill a list box using an SQL query?
I have a listbox that I need to populate with data from SQL.
I have a username/password/SQL address. SQL query would be (select StockNumber from dbo.VehicleMaster)

Would appreciate a code sample :)

Question by:Matrix1000
    LVL 24

    Expert Comment

    by:Jeff Certain
    Is your database SQL server or something else?
    LVL 34

    Accepted Solution

    dim conn as new sqlconnection("your connection string here")
    dim cmd as new sqlcommand
    dim dr as sqldatareader

    cmd.connection = conn
    cmd.commandtype = commandtype.text
    cmd.commandtext = "SELECT StockNumber FROM dbo.VehicleMaster ORDER BY StockNumber" 'ORDER BY clause is optional

         dr = cmd.executereader
         end while
    catch ex as SQLException (ex.message)
    end try
    LVL 24

    Assisted Solution

    by:Jeff Certain
    Sorry. Dumb question. The "dbo" should have been a giveaway...

    Dim conn as New System.Data.SqlClient.SQLConnection("insert your connection string here")
    Dim strSQL as string = "select StockNumber from dbo.VehicleMaster"
    Dim cmd as New System.Data.SQLClient.SQLCommand(strSQL, conn)
    Dim adapter As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter(cmd)
    Dim myTable as System.Data.DataTable = New System.Data.DataTable

    ListBox1.DataSource = myTable
    ListBox1.DataTextField =StockNumber
    ListBox1.DataValueField =StockNumber
    LVL 24

    Expert Comment

    by:Jeff Certain
    BriCrowe ,

    Nice hijack.
    LVL 1

    Author Comment

    BriCrowe's example worked perfectly right off the bat.

    Chaosian, had issues with
            lstStocknumberList.DataSource = myTable
            lstStocknumberList.DataTextField = StockNumber
            lstStocknumberList.DataValueField = StockNumber
     saying that 'DataTextField' etc is not a member of System.Windows.forms.Listbox.
    Thanks for trying tho :)

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    755 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

    22 Experts available now in Live!

    Get 1:1 Help Now