我正在尝试从.mdf表中选择所有值,并在Visual Studio下拉列表中列出它们。
表单加载时根本不会发生这种情况。
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";
}
有没有人有任何想法或能指出我哪里出错了?请。
答案 0 :(得分:2)
更改值成员,因为您没有从查询中选择Recipe_ID
发件人强>
recipeCombo.ValueMember = "Recipe_ID";
要强>
recipeCombo.ValueMember = "Recipe_Name";
或更改此类查询,
SqlCommand cmd = new SqlCommand("SELECT Recipe_Name,Recipe_ID FROM RECIPE", cn);