Select list item thenQuery Database with generic query?

Posted on 2005-05-05
Last Modified: 2006-11-18
I have a listbox with my stock#'s. I have a query string to SQL that can pull the data I need.

My question is how could I select a stock# on the list and automatically proform a query for the selected stock# details to return the year, make, model etc to some labels on my form.

Heres what I have so far for a connection and query...

Dim conn As New SqlConnection("My connection string;")
        Dim cmd As New SqlCommand
        Dim dr As SqlDataReader

        cmd.Connection = conn
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "SELECT StockNumber,Year,Make,Model,Color, Miles FROM dbo.VehicleMaster ORDER BY StockNumber"

            dr = cmd.ExecuteReader
            While dr.Read
            End While
        Catch ex As SqlException
        End Try
Question by:Matrix1000
    LVL 96

    Assisted Solution

    by:Bob Learned
    Which way are you looking to do it:

    Multiple columns?

    or labels?

    LVL 1

    Author Comment

    Well in the list box I want to show 4 items...but when you click on an item in the list it fills some textboxes with additional information from that Miles,VIN,Price, etc that is associated with that Stock#
    LVL 8

    Accepted Solution

    Hi Matrix,

    a couple of suggestions:

    1) Your first query could return only the info that goes into the listbox.
    When an item is selected, you retrieve the value ( .SelectedValue property, i believe), and execute another quey, passing that value as a parameter, to retrieve the data you need to populate the other txt boxes.

    This method may be slow as you are hitting the db twice, but it depends on the amonut of data in the db table.

    2) Select all the information in your first query, and use a dataAdapter to populate a dataset.
    Then, use a dataview object, and when user makes a selection, set the .Rowfilter property of the dataview. You can then set the text in your textboxos to the appropriate items in the dataview row.



    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video discusses moving either the default database or any database to a new volume.

    734 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

    25 Experts available now in Live!

    Get 1:1 Help Now