Solved

Populate combo box

Posted on 2009-05-05
8
187 Views
Last Modified: 2012-05-06
Please check the code below.  I included 2 functions.  The one that is under the form_load event works.  But I would like to do the same thing using ComboBoxLoad function that is below the first one.  I am having the problem finishing it.  Please help.

private void frmCustomer_Load(object sender, EventArgs e)
        {
            string[] states =
                {"Select state...",
                    "MI","OH","PA","VI"};
            foreach (string state in states)
                cmbStates.Items.Add(state);
            cmbStates.SelectedIndex = 0;
            ComboBoxLoad();
        }
       
        private void ComboBoxLoad()
        {
            string connectionString =
                    @"Data Source=MAIN-HOMEPC\SQLEXPRESS;Initial Catalog=KQC;Integrated Security=True;Pooling=False";
            SqlConnection myConn = new SqlConnection(connectionString);
            myConn.Open();
            string selectStates = "Select * from States";
            SqlDataAdapter stateDataAdapter;
            stateDataAdapter = new SqlDataAdapter(selectStates, myConn);
            DataSet stateDataSet = new DataSet();
            stateDataAdapter.Fill(stateDataSet);
0
Comment
Question by:Aquarus
  • 4
  • 3
8 Comments
 
LVL 9

Expert Comment

by:tculler
ID: 24310037
What exactly are you trying to achieve in the ComboBoxLoad method?
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24310163
 DataSet stateDataSet = new DataSet();
  stateDataAdapter.Fill(stateDataSet);

  cmbStates.DataProvider = stateDatatSet;
0
 

Author Comment

by:Aquarus
ID: 24317258
I have a table and the select statement that selects one field from this table.  I am populating the dataset with the result of this select statement and want the data to show in the dropdown list of the combo box. I do not have property DataProvider in my combox properties or methods. THis does not work:
 cmbStates.DataProvider = stateDatatSet;
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24318029

Sorry thinking of Flex

 try this

cmbStates.DataSource=stateDataSet.Tables[0];
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Aquarus
ID: 24319276
I did.  Here again my code with the last line as you suggested.  ATtached is the screenshot with the result.
ScreenShotMyCombo.doc
0
 
LVL 12

Accepted Solution

by:
williamcampbell earned 500 total points
ID: 24319854
Fill in the Table Column Name you want to display
add 
 

cmbStates.DisplayMember= ***NAME in TABLE ***;

cmbStates.ValueMember =comboBox1.DisplayMember;

Open in new window

0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24319860
maybe
 
cmbStates.DisplayMember= "State";
cmbStates.ValueMember =cmbStates.DisplayMember;

0
 

Author Closing Comment

by:Aquarus
ID: 31578262
Thank you williamcampbell
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now