Fill Combo box with values from Dataset

Shaitan00

Well-known member
Joined
Aug 11, 2003
Messages
343
Location
Hell
Given a Combo box [Cb] and Dataset [ds].
Dataset [ds] is a set of distinct values from a column in my SQL Table, I want to display this list in the combo box. Mimicking my methods in VB6 I did the following:
Cb.DataSource = ds;

However instead of displaying the list of Customers [which it does in VB6] I get the following as the first and only value of the combo box:
System.Data.DataViewManagerListItemTypeDescriptor

Anyclues?
 
You have to also set the DisplayMember property. If you set the DataSource to a DataSet, the DisplayMember should be "TableName.ColumnName". If you bind DataSource to a DataTable, then the DisplayMember can be the column name.

C#:
comboBox1.DataSource = myDataSet;
comboBox1.DisplayMember = "LookupTable.Val";

comboBox2.DataSource = myDataSet.Tables["LookupTable"];
comboBox1.DisplayMember = "Val";

-Ner
 
Doesnt give an error compiling but the result is the same.
Let me elaborate more, might help.

I have a Table called [Store], it houses 5 Columns [StoreName, StoreType, Bool1, Bool2, StoreID]. Each StoreName has 2 StoreTypes so I pass the following SQL Query and fill my Dataset with the results: "select distinct [StoreName] from [Store]"

Now I know my Dataset is ok because I can set it as Datasource of a Datagrid and the proper distinct StoreNames appear perfectly.

Using your approach I did the following:
Cb.DataSource = ds;
Cb.DisplayMember = "Store.StoreName";

And sadly I get the exact same result, my ComboBox is filled with 1 value and it is "System.Data.DataViewManagerListItemTypeDescriptor" again.

But I think we are on the right track, in VB6 I had to do something similar to get it to work.
 
Hey Shaitan00 & others,

Did you figure that problem out? :) If so, how did you fix it? I recently started a similar project and am getting the exact error.
Any help would be greatly appreciated.

Cheers,

digitalk
 
Dim dt As New DataTable("Table")
.
. Fill datatable here...know that the table fills with a column named "ColumnName"
.
dv = New DataView(dt)
Me.cmbSegCode.DataSource = dv
Me.cmbSegCode.DisplayMember = "ColumnName"

Works for me!
 
problems

Hello,

I still seem to be having problems so I thought it would be helpful to see the code.

*test is my dataset *
*conn is SQL connection string*

Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("", Conn1)

Conn1 = New OleDb.OleDbConnection(conn)
OleDbDataAdapter.Fill(test)

Dim custTables As DataTable = test.Tables.Add("CustTable")

Dim custDV As DataView = New DataView(test.Tables("CustTable"))

Dim custDV2 As DataView = test.Tables("CustTable").DefaultView

Me.ComboBox2.DataSource = custDV2
Me.ComboBox2.DisplayMember = "SITE"

The program runs through this after pressing a button but nothing is displayed. When it is pressed again, it crashes out with this error:

----------------------------------------
An unhandled exception of type System.Data.DuplicateNameException occurred in system.data.dll

Additional information: A DataTable named CustTable already belongs to this DataSet.
----------------------------------------

Any suggestions? Thoughts?

Thanks in advance. :)

Ciau...

Karl
 
The line:
Dim custTables As DataTable = test.Tables.Add("CustTable")

is adding a table with the name "CustTable" to your dataset. If you run through this code twice, youll be trying to create a table that already exists in the dataset. Thats your error.

What do you expect the following line to do:
OleDbDataAdapter.Fill(test)

You need to create an instance of OleDbDataAdapter, set its sql to something like "SELECT * FROM Table1" and THEN call Fill.

As far as I can see from your code, the object cmd isnt needed. You create a command object but dont set any properties and its never used (hopefully a compiler warning if its turned on). Maybe you thought you would fill the dataset from a command object? Cant be done...

-Ner
 
The dataset (test) was generated from the OleDbAdapter (Configure this adapter...) in VS.Net. This is the place where I created the sql query using a combination of the Query Builder and some custom sql. I see your point about how cmd line is not needed. :)

From here, I establish a connection, call the fill for the dataset (test), and then attempt to put the dataset into a table and display it.

Is this the way to view the dataset?

Thanks.

Karl
 
Binding a DataGrid to a DataSet is one way to view the data, sure. You can also use test.GetXml() to return a string of the XML version of the data. You could write it to a file with test.WriteXml(...) or just use something like "Debug.WriteLine(test.GetXml())" to view the XML in the Output window.

-Ner
 
Back for more...;)

At this point in our application development, I am not yet ready to work with xml but it is definatly a consideration for the future.

Heres what I am doing:

----------------
mscuhdbConn1 = New OleDb.OleDbConnection(conn)

(2)Dim custTables As DataTable = test.Tables.Add("New")

OleDbDataAdapter.Fill(test)

(1)ComboBox2.DataSource = test
(1)ComboBox2.DisplayMember = "site"

<OR>

(2)Dim dv As DataView = New DataView(test)
(2)ComboBox2.DataSource = dv
(2)ComboBox2.DisplayMember = "site"

----------------

The result of the #1 is that System.Data.DataViewManagerListItemTypeDescriptor message.

The result of the #2 doesnt do anything. Before I had tried creating a new table but I didnt have much luck with that.

Suggestions?

Thanks a bunch.

Karl
 
Back
Top