views:

41

answers:

1

Using VS2005 with C#

I want to fill the combobox by using the table value.

Code

OdbcConnection con = new OdbcConnection();
    OdbcCommand cmd;

con.ConnectionString =                            "";
        con.Open();
        cmd = new OdbcCommand("Select no from table", con);
        ada = new OdbcDataAdapter(cmd);
        ds = new DataSet();
        ada.Fill(ds);
        combobox1.Items.Add(ds);

But no values was loading in the combobox, what wrong with my above mentioned code.

can any provide a solution for the probelm....

+1  A: 

You do have something in your real connection string, right?

You're loading your data into a DataSet - that's a collection of tables and relations. How should the combobox know what table's data to display?? If the DataSet has multiple tables in it, you would have to additionally define which one of those to use. If the DataSet has only one table inside, then it's a waste of resources to use a DataSet in the first place.

If you only have a single set of data, use a DataTable instead:

con.Open();
cmd = new OdbcCommand("Select no from table", con);
ada = new OdbcDataAdapter(cmd);
DataTable data = new DataTable();
ada.Fill(data);

// define the column to be used to display text in the combobox
combobox1.DataTextField = "FirstName";
// define the column to be used as the value for the selection in the combobox
combobox1.DataValueField = "CustomerID";

combobox1.DataSource = data;
combobox1.DataBind();
marc_s
In a combobox showing System.Data.Datarowview instead of value
Gopal
@Gopal: updated my answer with two additional assignments
marc_s