Reputation: 1
could anyone help me please? Following my code and "An unhandled exception of type 'System.InvalidCastException' occurred in GelatoProject.exe
Additional information: Unable to cast object of type 'System.String' to type 'GelatoProject.RecipeVariables'." is the message.
// ...
using (var db = new GelatoProjectDBEntities())
{
RecipeVariables selected =
(RecipeVariables)comboBoxRecipeDetail.SelectedItem;
var results = (from x in db.Recipe_Parameters
select new RecipeVariables
{
Id = x.Id,
RecipeDetail = x.recipeDetail,
Parameter = x.parameter,
RangeDetail = x.rangeDetail,
RangeValue = x.value.ToString()
}
)
.Where(x => x.RecipeDetail == selected.RecipeDetail && x.Parameter == "totalsolids" && x.RangeDetail == "Max")
.FirstOrDefault();
totsolidsRangeMax = decimal.Parse(results.RangeValue);
MessageBox.Show(totsolidsRangeMax.ToString());
}
// ...
class RecipeVariables
{
public int Id { get; set; }
public string NameIngredient { get; set; }
public string Brand { get; set; }
public string LBName { get { return NameIngredient + " - " + Brand;}}
public string RecipeDetail { get; set; }
public string Parameter { get; set; }
public string RangeDetail { get; set; }
public string RangeValue { get; set; }
}
Upvotes: 0
Views: 1204
Reputation: 2701
RecipeVariables selected = (RecipeVariables)comboBoxRecipeDetail.SelectedItem;
comboBoxRecipeDetail.SelectedItem
is a string - text that you see when you clicked on the combobox. It can not be cast to RecipeVariables
Change your code to:
using (var db = new GelatoProjectDBEntities())
{
RecipeVariables selected = new RecipeVariables()
{
RecipeDetail = (string)comboBoxRecipeDetail.SelectedItem
};
// var results = ...
}
This will create a new RecipeVariables object and then set its RecipeDetail property to the text for selected combobox item.
Upvotes: 2