Click to See Complete Forum and Search --> : Fill Combo box with values from Dataset
Shaitan00
Aug 11th, 2003, 10:11 PM
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
Pirate
Aug 12th, 2003, 03:36 AM
XML structure and database represent the same thing . Instead of writing XML File name , you have to change that to your table name and add connection obj also .http://www.vbforums.com/showthread.php?s=&threadid=249117
Shaitan00
Aug 12th, 2003, 08:25 AM
Your method didn't generate any errors, however now the combobox is empty instead.
Code:
for(int i=0; i < ds.Tables.Count-1; i++){
this.Cb.Items.Add(ds.Tables[i].TableName);}
Note: Connection to server [connection object] has already been taken care of. I have my Dataset [ds] with all the information I need.
Using VB6 this was rather simple [and Data Combo Boxes]:
Set DCb.RowSource = ds
DCB.ListField = ds(0).Name
Pirate
Aug 12th, 2003, 08:52 AM
Ok , I've just created a quick demo that shows you how to load dataset content into combobox or listbox .
Shaitan00
Aug 13th, 2003, 07:41 AM
Sadly I am unable to test the demo [You seem to have a newer version of .Net then me].
However I was able to open the form and test the code.
When attempting to populate the Combo box I get the following error: "Cannot find Column 1" and then halts.
This error is not on Compile [code compiles perfectly]
Now I know my Dataset is ok because If I put a Datagrid and assign my Dataset as the Datasource, the Datagrid outputs exactly what I want in the combo box.
Pirate
Aug 13th, 2003, 07:47 AM
Does the database reside locally ? and are you using Access not SQL Server , just double checking ?
Shaitan00
Aug 13th, 2003, 09:27 AM
The Dataset houses the results from my SQL Query Select statement.
For Testing purposes the SQL Database is LOCALHOST but will not be when the application is launched [however this shouldn't make any difference, the Dataset has the proper results].
Pirate
Aug 13th, 2003, 09:31 AM
Post the code where you got the error in .
Shaitan00
Aug 13th, 2003, 09:51 AM
Code:
// Configure ComboBox
System.Data.DataSet ds = new System.Data.DataSet();
string strQuery = "select distinct [StoreName] from [Store]";
ds = m_oDB.GetRecords(strQuery); // Returns Dataset Result
//this.Cb.DataSource = oRecs; // Method that worked in VB6
foreach (DataRow dr in ds.Tables[0].Rows) // Your method
{
Cb.Items.Add(dr[0].ToString() + " -- " +
dr[1].ToString() + " -- " +
dr[2].ToString());
I also tried using the ideas you gave me before with the same basic code.
Pirate
Aug 13th, 2003, 09:55 AM
How many columns in your database while running this code ?
Shaitan00
Aug 13th, 2003, 10:37 AM
Not 100% sure how to answer that question.
I have a Table [Store] with 5 Columns [StoreName, StoreType, Mode1, Mode2, StoreID]. After my SQL Query Statment [posted below] I get a list of unique StoreNames in the DataSet.
For Example: Table [Store]:
StoreName StoreType Mode1 Mode2 StoreID
Mar ACX NO NO Unique Identifier
Mar BCX NO YES Unique Identifier
Walls ACX YES YES Unique Identifier
Walls XVX NO YES Unique Identifier
.........
Thus I want the Combobox to have 2 choices:
Mar
Walls
Does this answer your question?
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.