Connecting Tech Pros Worldwide Help | Site Map

Unusual problem when setting Combobox Display/Value members

  #1  
Old December 30th, 2008, 07:59 PM
Newbie
 
Join Date: Dec 2008
Location: Bloominton, CA
Posts: 30
Using this code, the display member of the combo box is displaying the text "System.Data.DataViewManagerListItemTypeDescriptor ". 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.

Expand|Select|Wrap|Line Numbers
  1. Public Function FillFromAccess(ByVal QueryString As String, ByVal FillTable As String)
  2.  
  3.         Dim OLEConnString As String
  4.         OLEConnString = "Provider=" & My.Settings.Connection_AccessProvider & ";" _
  5.             & "Data Source=" & My.Settings.Connection_Location & ";" ' _
  6.         '   & "Initial Catalog=Blah;" _
  7.         '   & "User Id=username;" _
  8.         '   & "Password='password';"
  9.  
  10.         Dim DBConn As New OleDbConnection(OLEConnString)
  11.  
  12.         Dim DBCommand As OleDbDataAdapter
  13.         Dim DataSetAccess As New DataSet
  14.  
  15.         ' For any data needed, pull it out of the database.
  16.         DBCommand = New OleDb.OleDbDataAdapter(QueryString, DBConn)
  17.         ' Once that's ready, use it to fill the dataset DSPageData:
  18.         DBCommand.Fill(DataSetAccess, FillTable)
  19.  
  20.         FillFromAccess = DataSetAccess
  21.  
  22.     End Function
Expand|Select|Wrap|Line Numbers
  1. Private Sub Add_action_items_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         'Load the job number into the job number combo box
  3.         Dim SQL As String
  4.         Dim DataSet_Jobs_Incomplete As New DataSet
  5.  
  6.         'Find the information from the database
  7.         SQL = "Select * From Jobs WHERE Completed <> True"
  8.         DataSet_Jobs_Incomplete = FillFromAccess(SQL, "Jobs_Incomplete")
  9.  
  10.         'Add the list of job numbers to the combo box
  11.         With CmbJobNum
  12.             .DataSource = DataSet_Jobs_Incomplete
  13.             '.ValueMember = "ID"
  14.             .DisplayMember = "Job_num"
  15.             .SelectedIndex = 0
  16.         End With
  17.  
  18.     End Sub
  #2  
Old December 30th, 2008, 09:39 PM
Newbie
 
Join Date: Dec 2008
Location: Bloominton, CA
Posts: 30

re: Unusual problem when setting Combobox Display/Value members


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.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Add_action_items_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         'Load the job number into the job number combo box
  3.         Dim SQL As String
  4.         Dim DataSet_Jobs_Incomplete As New DataSet
  5.  
  6.         'Find the information from the database
  7.         SQL = "Select * From Jobs WHERE Completed <> True"
  8.         DataSet_Jobs_Incomplete = FillFromAccess(SQL, "Jobs_Incomplete")
  9.  
  10.         'Add the list of job numbers to the combo box
  11.         With CmbJobNum
  12.             .DataSource = DataSet_Jobs_Incomplete.Tables(0)
  13.             .ValueMember = "ID"
  14.             .DisplayMember = "Job_num"
  15.             .SelectedIndex = 0
  16.         End With
  17.  
  18.     End Sub
  #3  
Old December 31st, 2008, 07:17 PM
Newbie
 
Join Date: Dec 2008
Location: Bloominton, CA
Posts: 30

re: Unusual problem when setting Combobox Display/Value members


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.Index = 0) when their index doesn't actually exist.

I hope my past mistakes can be put to use by someone else...
Expand|Select|Wrap|Line Numbers
  1.         Try
  2.             DBCommand.Fill(DataSetAccess, FillTable)
  3.         Catch ex As Exception
  4.             FillFromAccess = Nothing
  5.             Exit Function
  6.         End Try
  7.  
Reply

Tags
combobox, dataset, datatable, displaymember, valuemember