0

I am trying to populate a combobox using a DataTable returned from SQL via SQLDataAdapter.

The combobox entries should display as the strings from the VendorName column but they are showing as the Integers from the ID column.

Can anyone spot what I've missed?

DataTable vendors = sql.GetData("SELECT id, VendorName FROM Vendors ORDER BY VendorName", "Q");

drpVendor.DataSource = vendors;
drpVendor.DisplayMember = "VendorName";
drpVendor.ValueMember = "id";
drpVendor.SelectedIndex = 0;

enter image description here

I have verified the contents of the DataTable vendors by assigning it as a the DataSource of a DataGridView as shown below:

enter image description here

Stef Joynson
  • 222
  • 3
  • 10
  • We probably would have to see what you are doing in the GetData function. – LarsTech Nov 25 '15 at 18:17
  • The problem is not in the code you have shown us. If DisplayMember was not set correctly, it would be showing something like `System.Data.DataRow` in your combobox. You must be doing something wrong in `sql.GetData()`. – Steve Nov 25 '15 at 18:23

1 Answers1

0

The root of the problem here was this issue with Visual Studio 2010 not displaying runtime errors. Once that was corrected in Debug > Exceptions > CLR ... the runtime errors allowed me to identify the following problem.

drpVendor.DataSource = vendors;

After this line is called the drpVendor_SelectedIndexChanged event was being fired which was trying to alter a read only column of a DataGridView.

As CLR exceptions were not being thrown the code just crashed out after this line leaving the combobox displaying the default "id" column.

Community
  • 1
  • 1
Stef Joynson
  • 222
  • 3
  • 10