Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to ask you how to fill combo box using c# and in sql server
Posted
Comments
Ashishmau 11-Apr-11 5:18am    
Its very basic things...have u tried it once

Try this.

Con = new SqlConnection("yOUR cONNECTION sTRING ");
            Con.Open();
             Cmd = new SqlCommand("Select * from Products");
             dt = new SqlDataAdapter("Select ProductId,ProductName+ProductPrice as Name from products", Con);
              DataSet ds = new DataSet();
              dt.Fill(ds);
              DropDownList1.DataSource = ds;
              DropDownList1.DataTextField = Convert.ToString(ds.Tables[0].Rows[0]["Name"]);
              DropDownList1.DataValueField = Convert.ToString(ds.Tables[0].Rows[0]["ProductId"]);
              DropDownList1.DataBind();
            Con.Close();
 
Share this answer
 
string connecttionstring = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
            SqlConnection cs1 = new SqlConnection(connecttionstring);
            SqlCommand cmd1 = new SqlCommand();
            cs1.Open();
            cmd1.CommandText = "your select Query";
            cmd1.Connection = cs1;
            DataSet dtst = new DataSet();
            SqlDataAdapter dp = new SqlDataAdapter(cmd1);
            dp.Fill(dtst);// for select query
            DropDownList1.DataSource = dtst;
            DropDownList1.DataTextField = "Value you want to show";//this value should be there in your select query
            DropDownList1.DataValueField = "Value you want to select ";//this value should be there in your select query
            DropDownList1.DataBind();
 
Share this answer
 
v2
 
Share this answer
 
try this
SqlCommand cmd = new SqlCommand("yourquery", yourConnection);
           SqlDataReader dr = cmd.ExecuteReader();

while (dr.Read())
            {
                comboBox1.Items.Add(dr[Index].ToString());
            }
            dr.Close();

and
C#
private void BindComboBox()
{
  comboBox1.DataSource = dataSet1.Tables["TableName"];
  comboBox1.DisplayMember = "ColumnName";
}


Take a look there-[Bind a Windows Forms ComboBox or ListBox Control to Data][^] for details.
 
Share this answer
 
v2
My sql server Table is:-
User_Id Name
1 Prince
2 Rakesh
3 Rahul

Now my table name is Student

-----
In C# Windows application:-
Declare the page load event
SqlConnection con = new SqlConnection(//Connection string);
      SqlDataAdapter     adpter = new SqlDataAdapter("Select * from cUser", con);
           DataSet ds = new DataSet();
           adpter.Fill(ds);
           comboBox1.DataSource = ds.Tables[0];
           comboBox1.DisplayMember = "Name";
           comboBox1.ValueMember = "User_Id";


In above solution ComboBox1 is my combobox


In another way of you use go to combobox and choose the datasource property and add project data source

now you select the displaymember property for which you want to display. also ValueMamber property use for display value.

3rd Method

SqlConnection con = new SqlConnection(//Connection string);
       SqlDataAdapter     adpter = new SqlDataAdapter("Select * from cUser", con);
            DataSet ds = new DataSet();
            adpter.Fill(ds);
           for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                comboBox1.Items.Add(ds.Tables[0].Rows[i][0].ToString());
            }
 
Share this answer
 
v2
C#
public void fill_groups()
        {

            try
            {
                sqlConnection  con = new sqlConnection (connectionstring);
                DataSet ds = new DataSet();
                sqlDataAdapter  ad = new sqlDataAdapter ("select  groupname from groupNames ", con);
                ad.Fill(ds);
                cmbgroup.Items.Add("---Select---");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    cmbgroup.Items.Add(ds.Tables[0].Rows[i][0].ToString());

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
          
        }
 
Share this answer
 
v2
C#
SqlCommand cmd = new SqlCommand("yourquery", yourConnection);
           SqlDataReader dr = cmd.ExecuteReader();
 
while (dr.Read())
            {
                comboBox1.Items.Add(dr.Getstring(0).ToString());
            }
            dr.Close();
 
Share this answer
 
v2
 
Share this answer
 

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