Unusual problem when setting Combobox Display/Value members

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Infog
    New Member
    • Dec 2008
    • 36

    Unusual problem when setting Combobox Display/Value members

    Using this code, the display member of the combo box is displaying the text "System.Data.Da taViewManagerLi stItemTypeDescr iptor". Huh???

    The first block of code gets the data from the database, and works as far as I know. The second block of code sets the first value of the form, and does NOT work like I want it too. I commented out the line that sets the value member, because it gives an exception.

    Code:
    Public Function FillFromAccess(ByVal QueryString As String, ByVal FillTable As String)
    
            Dim OLEConnString As String
            OLEConnString = "Provider=" & My.Settings.Connection_AccessProvider & ";" _
                & "Data Source=" & My.Settings.Connection_Location & ";" ' _
            '   & "Initial Catalog=Blah;" _
            '   & "User Id=username;" _
            '   & "Password='password';"
    
            Dim DBConn As New OleDbConnection(OLEConnString)
    
            Dim DBCommand As OleDbDataAdapter
            Dim DataSetAccess As New DataSet
    
            ' For any data needed, pull it out of the database.
            DBCommand = New OleDb.OleDbDataAdapter(QueryString, DBConn)
            ' Once that's ready, use it to fill the dataset DSPageData:
            DBCommand.Fill(DataSetAccess, FillTable)
    
            FillFromAccess = DataSetAccess
    
        End Function
    Code:
    Private Sub Add_action_items_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Load the job number into the job number combo box
            Dim SQL As String
            Dim DataSet_Jobs_Incomplete As New DataSet
    
            'Find the information from the database
            SQL = "Select * From Jobs WHERE Completed <> True"
            DataSet_Jobs_Incomplete = FillFromAccess(SQL, "Jobs_Incomplete")
    
            'Add the list of job numbers to the combo box
            With CmbJobNum
                .DataSource = DataSet_Jobs_Incomplete
                '.ValueMember = "ID"
                .DisplayMember = "Job_num"
                .SelectedIndex = 0
            End With
    
        End Sub
  • Infog
    New Member
    • Dec 2008
    • 36

    #2
    I found the problem! I feel kind of silly now.

    I didn't know how to reference which table in the dataset to use, and I had swapped the valuemember and displaymember.

    The code below works to set a combobox to a list, pulled from a database.

    Code:
    Private Sub Add_action_items_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Load the job number into the job number combo box
            Dim SQL As String
            Dim DataSet_Jobs_Incomplete As New DataSet
    
            'Find the information from the database
            SQL = "Select * From Jobs WHERE Completed <> True"
            DataSet_Jobs_Incomplete = FillFromAccess(SQL, "Jobs_Incomplete")
    
            'Add the list of job numbers to the combo box
            With CmbJobNum
                .DataSource = DataSet_Jobs_Incomplete.Tables(0)
                .ValueMember = "ID"
                .DisplayMember = "Job_num"
                .SelectedIndex = 0
            End With
    
        End Sub

    Comment

    • Infog
      New Member
      • Dec 2008
      • 36

      #3
      More advice, for whoever may be researching this topic later -

      If the correctly formed query you send to the database returns no records, VB.NET will give an exception. To stop that, catch the exception and send back the value Nothing. Now, it skips the exception, and you can test for Nothing in the sub that used the above function. That way, no controls are set to an index (ComboBox1.Inde x = 0) when their index doesn't actually exist.

      I hope my past mistakes can be put to use by someone else...
      Code:
              Try
                  DBCommand.Fill(DataSetAccess, FillTable)
              Catch ex As Exception
                  FillFromAccess = Nothing
                  Exit Function
              End Try

      Comment

      Working...