Results 1 to 11 of 11

Thread: Fill Combo box with values from Dataset

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308

    Question Fill Combo box with values from Dataset

    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

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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.p...hreadid=249117

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308
    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
    Last edited by Shaitan00; Aug 12th, 2003 at 08:31 AM.

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Ok , I've just created a quick demo that shows you how to load dataset content into combobox or listbox .
    Attached Files Attached Files

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308
    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.

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Does the database reside locally ? and are you using Access not SQL Server , just double checking ?

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308
    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].

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Post the code where you got the error in .

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308
    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.

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    How many columns in your database while running this code ?

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308
    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width