I have two controls on a Windows Form - A Combobox and a ListBox.
I'm trying to populate the ListBox based on selected value from the ComboBox, but I keep getting the following error.
Unable to cast object of type 'System.Data.Entity.DynamicProxies.Category_B0496327038CB6B25A6EF7B750129DD7B44A87BE41C4E2DB0F8D7A00898B060F' to type 'System.IConvertible'.
public partial class Form1 : Form
{
NorthWindEntities dbContext = new NorthWindEntities();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cbx_Categories.DataSource = dbContext.Categories.ToList();
cbx_Categories.DisplayMember = "CategoryName";
cbx_Categories.ValueMember = "CategoryID";
}
private void cbx_Categories_SelectedIndexChanged(object sender, EventArgs e)
{
int categoryID = Convert.ToInt16(cbx_Categories.SelectedValue);
PopulateProductsLbx(categoryID);
}
void PopulateProductsLbx(int categoryID)
{
var products = from p in dbContext.Products
where p.CategoryID == categoryID
select p.ProductName;
lbx_Products.DataSource = products.ToList();
}
}
What would be my best approach to resolve this? Thank you in advance.