Dispaly First and Last Name in a Combo Box

Posted on 2005-04-20
Last Modified: 2010-04-23
FYI I'm new to VB.Net.

I have a form which is connected to a database.  The fields are displaying on the form in textboxes but in the drop down list box I need to select the persons name to go to there record.  My problem is I can only tie one field to the combo box.  How do I display the first and last name in the list box?

Question by:running32
    LVL 13

    Expert Comment

    You could populate the list box dynamically by concatinating the first name with the last name into a string and then adding it as an item to the listbox.

    Author Comment

    I don't know how to do that.   could you give me an example.

    I'm guessing I have to use the display memeber property but I do not know how.

    LVL 28

    Expert Comment

    read the SQL result to the datareader and then populate combo from datareader

    dim cn as OleDbConnection
    Dim cmd As New OleDbCommand()
    Dim dr as OledbDataReader
    Dim sSQL as String = "select FName , LName from Table"
    cn = New OledbConnection("Provider=Microsoft.Jet.Oledb.4.0;Source=<path to your database>")
    with cmd
        .Connection = cn
        .CommandText = sSQL
        .CommandTimeout = 0
        dr = .ExecuteReader()
    end with
            While dr.Read()
                cmb.Items.Add(dr("FName").ToString.Trim & dr("lName").ToString.Trim)
     End While

    LVL 13

    Accepted Solution

    Ok, if you are using SQL you would do it like this.  

    Say you have already filled your dataset "dsTemp" with your first and last names of data.

    Dim mystring, myfirstname, mylastname As String
    Dim RecordCount, RecordIndex as integer

     RecordCount = dsTemp.Tables(0).Rows.Count

    While (RecordCount > RecordIndex)
                If Not (dsTemp.Tables(0).Rows(RecordIndex).Item("FirstName") Is System.DBNull.Value) Then
                    myFirstName = dsTemp.Tables(0).Rows(RecordIndex).Item("FirstName")
                End If
                If Not (dsTemp.Tables(0).Rows(RecordIndex).Item("LastName") Is System.DBNull.Value) Then
                    myLastName = dsTemp.Tables(0).Rows(RecordIndex).Item("LastName")
                End If

                myString = myFirstName & " " & myLastName


                RecordIndex += 1

    End While

    Author Comment


    thanks Thats what I needed.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    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…
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 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

    20 Experts available now in Live!

    Get 1:1 Help Now