Maiko Fujimaru
Maiko Fujimaru

Reputation: 15

Populating a listbox with 2 columns from database

i want to populate a listbox with 2 columns from database

Only 1 column is displaying in my listbox

I want to display FistName and LastName from my database but only FistName is showing.

Here are my codes:

 Try
        connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Nikko Jaze Fabellon\Documents\ASRASIM.accdb")
        connection.Open()
        ds = New DataSet
        tables = ds.Tables
        dataAdapter = New OleDbDataAdapter("SELECT [FirstName],[LastName] from [Personnel] where [Status] = 'Activated' ", connection)
        dataAdapter.Fill(ds, "Personnel")
        Dim view1 As New DataView(tables(0))
        With personnelList
            .DataSource = ds.Tables("Personnel")
            .DisplayMember = "FirstName"
            .ValueMember = "ID"
            .SelectedIndex = 0
        End With

        connection.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

Upvotes: 1

Views: 835

Answers (1)

Steve
Steve

Reputation: 216243

Just force your query to return both fields joined together in this way (And do not forget the field ID used in the ValueMember)

Dim cmdText = "SELECT ID, [FirstName] & ' ' & [LastName] as FullName 
               FROM [Personnel] 
               WHERE [Status] = 'Activated'"

dataAdapter = New OleDbDataAdapter(cmdText, connection)
dataAdapter.Fill(ds, "Personnel")
Dim view1 As New DataView(tables(0))
With personnelList
    .DataSource = ds.Tables("Personnel")
    .DisplayMember = "FullName"
    .ValueMember = "ID"
    .SelectedIndex = 0
End With

Notice that this syntax is valid for MS-Access. I am not sure if you can use the same syntax to concatenate two fields in other database systems.

Upvotes: 1

Related Questions