Click here to Skip to main content
15,886,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

I wrote the below code for a comboBox

C#
if (comboBox1.Text == "Name")
            {
                SqlCommand cmd = new SqlCommand("select name from details",con);
                con.Open();


Now i want to add the result to another comboBox

Suppose the result of the query comes as
a
b
c
then it should show in the second comboBox..

please tell me how to bind the result to the second comboBox
Posted

C#
if (comboBox1.Text == "Name")
            {
                string cmd = "select * from details";
                SqlDataAdapter da = new SqlDataAdapterh(cmd, con);
                DataTable dt =  new DataTable();
                dah.fill(dt);

                foreach(DataRow dr in dt.Rows)
                {
                   comboBox2.Items.Add(dr["name"].ToString());
                }
            }
 
Share this answer
 
Firstof all you need to execute your command object. You can achieve that with Execute method. A full example follows:

SQL
public DateTime GetServerDate()
        {
            using (SqlConnection cn = new SqlConnection(ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand(@"SELECT GETDATE()", cn))
                {
                    cn.Open();

                    return (DateTime)cmd.ExecuteScalar();
                }
            }
        }


In this example we are calling ExecuteScalar method. It will return only the first column of first row of the fetched data. More information about it you can find here: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx[^]
SqlCommand object offers also other methods as ExecuteReader() and ExecuteNonQuery().
Basically you will call ExecuteReader() method and then bind the result to your combo.

cscarp
[DataObject(true)]
public class DataService
{
/// <summary>
/// Retrives all the detail data
/// </summary>
/// <returns>Datatable contining the requested data.</returns>
public static DataTable GetAllByDetail()
{
    DataTable table = new DataTable();

    using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand("select name from details", cn))
        {
            cn.Open();

            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                table.Load(reader);
            }
        }
    }

    return table;
}
}


Now you can call this method, and bind the data to your combo.

C#
private DataTable dt = DataService.Retrieve();

ComboBox1.DataSource = dt; 
ComboBox1.DisplayMember = "Name"; 
ComboBox1.ValueMember = "Name";


I hope this answers all of your doubts.

Cheers
 
Share this answer
 
v2

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