• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

Dispaly First and Last Name in a Combo Box

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?

Thanks
0
running32
Asked:
running32
  • 2
  • 2
1 Solution
 
TorrwinCommented:
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.
0
 
running32Author Commented:
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.

Thanks
0
 
iboutchkineCommented:
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>")
cn.Open()
with cmd
    .Connection = cn
    .CommandText = sSQL
    .CommandTimeout = 0
    dr = .ExecuteReader()
end with
cmb.Items.Clear()
        While dr.Read()
            cmb.Items.Add(dr("FName").ToString.Trim & dr("lName").ToString.Trim)
 End While

dr.Close()
cn.Close()
0
 
TorrwinCommented:
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

            Listbox1.items.add(mystring)

            RecordIndex += 1

End While
0
 
running32Author Commented:

thanks Thats what I needed.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now