Reputation: 43
I have a method that is called when the main form is opened
private void populateComboBoxes()
{
SqlCeDataAdapter breakfastAdapter = new SqlCeDataAdapter("SELECT DISTINCT recipeName FROM Recipe WHERE Category = 'breakfast' ", databaseConnection);
SqlCeDataAdapter lunchAdapter = new SqlCeDataAdapter("SELECT DISTINCT recipeName FROM Recipe WHERE Category = 'lunch' ", databaseConnection);
breakfastAdapter.Fill(breakfastDS, "Recipe");
lunchAdapter.Fill(lunchDS, "Recipe");
cmbBox1.DisplayMember = "recipeName";
cmbBox1.ValueMember = "recipeName";
cmbBox1.DataSource = breakfastDS.Tables["Recipe"];
cmbBox2.DataSource = lunchDS.Tables["Recipe"];
cmbBox2.DisplayMember = "recipeName";
cmbBox2.ValueMember = "recipeName";
}
which essentially populates the two comboboxes based on the SELECT statements. Once this method hits cmbBox1.DataSource = breakfastDS.Tables["Recipe"]
it stops executing and moves to this method:
private void cmbBox1_SelectedIndexChanged(object sender, EventArgs e)
{
rtbPicture.Visible = false;
string qry = "";
qry = "SELECT DISTINCT ingredientName FROM Recipe WHERE recipeName = " + cmbBox1.SelectedItem.ToString();
SqlCeCommand com = new SqlCeCommand(qry, databaseConnection);
com.CommandText = qry;
rtbRecipe.Text = com.ExecuteScalar().ToString();
}
This method is supposed to execute the select statement and put that information in the richtextbox
, but for some reason cmbBox1
isn't set. From my understanding the first combobox
already had set the display and value members in the previous method. However, when I get to the cmbBox1.SelectedItem.ToString()
it returns System.Data.DataRowView
instead of the actual string in the combobox
. I'm not sure why it's giving me System.Data.DataRowView
instead of the string.
Upvotes: 1
Views: 11031
Reputation: 1
cmbBox2.DisplayMember = "recipeName";
cmbBox2.DataSource = lunchDS.Tables["Recipe"];
cmbBox2.ValueMember = "recipeName";
Upvotes: 0
Reputation: 16433
You are using cmbBox1.SelectedItem.ToString()
.
You should probably be using SelectedValue
instead:
qry = "SELECT DISTINCT ingredientName FROM Recipe WHERE recipeName = " + cmbBox1.SelectedValue.ToString();
SelectedItem
gets the actual ComboBox
item (in this case a DataRowView
), whereas SelectedValue
gets the value of the property you specified as the ValueMember
of the ComboBox
.
Upvotes: 3