Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Comments
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.

Try this:
C#
string strConn = "Data Source=SEZSW08;Initial Catalog=Nidhi;Integrated Security=True";
        SqlConnection Con = new SqlConnection(strConn);
        Con.Open();
        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();
        Con.Close();
        da.Fill(ds);
        cmbCompName.DataSource = ds;
        cmbCompName.DisplayMember = "CompanyName";
        cmbCompName.ValueMember = "CompanyName";
        //cmbCompName.DataBind();
        cmbCompName.Enabled = true;

If it is a drop down then use
C#
ddlCompName.Datasource=ds;
ddlCompName.Datavaluefield="CompanyName";
ddlCompName.DataTextfield="CompanyName";
ddlCompName.DataBind();

[Edit]Code blocks added[/Edit]
 
Share this answer
 
v2
Comments
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.
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
            cn.Open();

            //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
            dAdapter.Fill(dt);
            //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";

        }</pre>
 
Share this answer
 
Comments
JayantaChatterjee 24-Feb-13 12:00pm    
5 vote from me...
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.
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900