Sky Scraper
Sky Scraper

Reputation: 185

vb.net combobox shows only 1 record

when i add cmname clname on .ValueMember i'm having error, because im planning to show 3 records in asingle combobox, i.e. Will a. Smith

Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    con.ConnectionString = ("server=localhost;user id=root;database=db")
    Try
        con.Open()
        With cmd
            .Connection = con
            .CommandText = "SELECT cfname, cmname, clname from candidate;"
        End With
        Dim dt As New DataTable
        With ComboBox1
            da.SelectCommand = cmd
            da.Fill(dt)
            .DataSource = dt
            .DisplayMember = "cfname"
            .ValueMember = "cfname"
        End With
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    con.Close()
End Sub

Upvotes: 2

Views: 212

Answers (1)

Steven Doggart
Steven Doggart

Reputation: 43743

You can concatenate the values from the columns in your SQL command, like this:

.CommandText = "SELECT CONCAT_WS(' ', cfname, cmname, clname) AS fullname FROM candidate;"

Then, set your DisplayMember and ValueMember properties to that concatenated column, like this:

.DisplayMember = "fullname"
.ValueMember = "fullname"

Upvotes: 3

Related Questions