Click here to Skip to main content
13,092,310 members (59,727 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I dont know how to populate combobox in c# by resault set of select statement of sql
for example for gender male and female. plz help me tanx
Posted 23-Feb-13 21:37pm
Sandeep Mewara 24-Feb-13 4:31am
This is not a well framed question! We cannot work out what you are trying to do/ask from the post. Please elaborate and be specific.
Use the "Improve question" link to edit your question and provide better information.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Try this:
string strConn = "Data Source=SEZSW08;Initial Catalog=Nidhi;Integrated Security=True";
        SqlConnection Con = new SqlConnection(strConn);
        string strCmd = "select companyName from companyinfo where CompanyName='" + cmbCompName.SelectedValue + "';";
        SqlCommand Cmd = new SqlCommand(strCmd, Con);
        SqlDataAdapter da = new SqlDataAdapter(strCmd, Con);
        DataSet ds = new DataSet();
        cmbCompName.DataSource = ds;
        cmbCompName.DisplayMember = "CompanyName";
        cmbCompName.ValueMember = "CompanyName";
        cmbCompName.Enabled = true;

If it is a drop down then use

[Edit]Code blocks added[/Edit]
osama.javed 28-Jul-13 5:51am
how can i add 'Select Value from combobox' at first index of combo box in this approach.
Member 10976838 5-Aug-14 6:08am
how can i add 'Select Value from combobox' at first index of combo box in this approach.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Try Something like this

<pre> private void Form1_Load(object sender, EventArgs e)
            //Making Sql Connection
            SqlConnection cn = new SqlConnection();
            //Setting Connection String Property
            cn.ConnectionString = "data source = (local); initial catalog = TestDB; integrated security =sspi";
            //Opening Connection
            //Creating Sql Command
            SqlCommand cmd = new SqlCommand();
            //Creating String Variable for SQL Command CommandProperty
            string sqlQuery = "select GenderID,GenderType from Gender";
            //Passing Query and Connection to the SQL Command
            cmd = new SqlCommand(sqlQuery, cn);
            //Creating Sql Data Adapter
            SqlDataAdapter dAdapter = new SqlDataAdapter();
            //Creating Data Table
            DataTable dt = new DataTable();
            //Initializing SQL Data Adapter Command Property
            dAdapter.SelectCommand = cmd;
            //Filling Data Table
            //Populating Combo Box from Data Table
            cmbGender.DataSource = dt;
            //Setting Combo Box ValueMember Property
            cmbGender.ValueMember = "GenderID";
            //Setting Combo Box DisplayMember Property
            cmbGender.DisplayMember = "GenderType";
Jayanta Chatterjee 24-Feb-13 12:00pm
5 vote from me...
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

In practice you should not write any sql statement/execute database command from directly your presentation layer. You should write all that in dataaccess layer and return custom entity from that layer. From presentation layer you get that data by Business Layer and bind it with your drop down list.
Hodamdi 26-Feb-13 1:49am
Tanks for your solutions

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 24 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100