Nick
Nick

Reputation: 51

ComboBox not showing DisplayMember

I'm trying to display the Question ID's in the combo box, in order to reproduce the matching question in a text box. However rather than the Question ID's appearing, I am receiving this for all 5 question ID's:

WCInterface.ucQuestions+QuestionWCInterface.ucQuestions+Question

My code:

Private loaded As Boolean = False

Private Sub ucQuestions_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    cmbQuestion.DisplayMember = "Question_ID"
    cmbQuestion.ValueMember = "Question_ID"
    cmbQuestion.DataSource = retrieveQuestions() 'when form loads

    loaded = True

End Sub

Private Sub cmbQuestion_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbQuestion.SelectedIndexChanged
    If (loaded) Then

        cmbQuestion.DisplayMember = "Question_ID"
        cmbQuestion.ValueMember = "Question_ID"
        cmbQuestion.DataSource = Nothing 'Resets data source
        cmbQuestion.DataSource = retrieveQuestions() 'when form loads
    End If
End Sub

Public Function retrieveQuestions() As List(Of Question)

    Dim typeList As New List(Of Question)
    Dim Str As String = "SELECT Question_ID, Question_Text FROM Question"
    Try
        Using conn As New SqlClient.SqlConnection(DBConnection)
            conn.Open()
            Using cmdQuery As New SqlClient.SqlCommand(Str, conn)
                Using drResult As SqlClient.SqlDataReader = cmdQuery.ExecuteReader()
                    While drResult.Read
                        typeList.Add(New Question(drResult("Question_ID"), drResult("Question_Text")))
                    End While
                End Using 'Automatically closes connection
            End Using
        End Using

    Catch ex As Exception

        MsgBox("Question List Exception: " & ex.Message & vbNewLine & Str)

    End Try

    Return typeList

End Function

I'd appreciate any suggestions as to how I display the Question ID's, thankyou

Upvotes: 1

Views: 825

Answers (2)

LarsTech
LarsTech

Reputation: 81610

You didn't post your Question class, but the DisplayMember and ValueMember fields aren't matching the property fields in the Question class:

It should look something like this:

Public Class Question
  Property QuestionID As Integer
  Property QuestionText As String

  Public Sub New(q_ID As Integer, q_Text As String)
    QuestionID = q_ID
    QuestionText = q_Text
  End Sub
End Class

Then your data source properties would look like this:

cmbQuestion.DisplayMember = "QuestionID"
cmbQuestion.ValueMember = "QuestionID"
cmbQuestion.DataSource = retrieveQuestions() 

Upvotes: 1

ArielEsquina
ArielEsquina

Reputation: 1

You must first assign the datasource and then assign the field to show

cmbQuestion.DataSource = Nothing 'Resets data source

cmbQuestion.DataSource = retrieveQuestions() 'when form loads

cmbQuestion.DisplayMember = "Question_ID"

cmbQuestion.ValueMember = "Question_ID"

Upvotes: 0

Related Questions