D B
D B

Reputation: 306

ComboBox not listing values selected from a table

I am trying to select all values from a .mdf table and list them in Visual Studio drop down list.

This is not happening at all on form load.

using (var cn = new SqlConnection(MY CONNECTION STRING))
{
    cn.Open();
    DataTable dt = new DataTable();

    try
    {
        SqlCommand cmd = new SqlCommand("SELECT Recipe_Name FROM RECIPE", cn);
        SqlDataReader myReader = cmd.ExecuteReader();
        dt.Load(myReader);
    }
    catch (SqlException e)
    {
        Console.WriteLine(e.ToString());
        return;
    }

    recipeCombo.DataSource = dt;
    recipeCombo.ValueMember = "Recipe_ID";
    recipeCombo.DisplayMember = "Recipe_Name";
}

Does anyone have any ideas or can point me at where I am going wrong? Please.

Upvotes: 0

Views: 30

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222572

Change Value member since you are not selecting Recipe_ID from the query

From

 recipeCombo.ValueMember = "Recipe_ID";

To

 recipeCombo.ValueMember = "Recipe_Name";

or change the query like this,

 SqlCommand cmd = new SqlCommand("SELECT Recipe_Name,Recipe_ID FROM RECIPE", cn);

Upvotes: 2

Related Questions