Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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
Hodamdi315
Comments
Sandeep Mewara at 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
good
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);
        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
ddlCompName.Datasource=ds;
ddlCompName.Datavaluefield="CompanyName";
ddlCompName.DataTextfield="CompanyName";
ddlCompName.DataBind();
[Edit]Code blocks added[/Edit]
  Permalink  
v2
Comments
osama.javed at 28-Jul-13 5:51am
   
how can i add 'Select Value from combobox' at first index of combo box in this approach.
Member 10976838 at 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
good
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
            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>
  Permalink  
Comments
Jayanta Chatterjee at 24-Feb-13 12:00pm
   
5 vote from me...
Rate this: bad
good
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.
  Permalink  
Comments
Hodamdi at 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
0 OriginalGriff 6,656
1 Sergey Alexandrovich Kryukov 6,400
2 CPallini 5,240
3 George Jonsson 3,584
4 Gihan Liyanage 2,625


Advertise | Privacy | Mobile
Web01 | 2.8.140921.1 | Last Updated 24 Feb 2013
Copyright © CodeProject, 1999-2014
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