Click here to Skip to main content
12,350,134 members (26,520 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# Windows
Hi all

I wrote the below code for a comboBox

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 7-Dec-12 20:49pm
Code-Hunt7.1K
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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());
                }
            }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Firstof all you need to execute your command object. You can achieve that with Execute method. A full example follows:

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.

[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.

private DataTable dt = DataService.Retrieve();
 
ComboBox1.DataSource = dt; 
ComboBox1.DisplayMember = "Name"; 
ComboBox1.ValueMember = "Name";

I hope this answers all of your doubts.

Cheers
  Permalink  
v2

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 | Mobile
Web02 | 2.8.160621.1 | Last Updated 8 Dec 2012
Copyright © CodeProject, 1999-2016
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