Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Populate combo box

Posted on 2009-05-05
8
Medium Priority
?
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:williamcampbell
ID: 24318029

Sorry thinking of Flex

 try this

cmbStates.DataSource=stateDataSet.Tables[0];
0
 

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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

636 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