My Windows form has three comboboxes. CmdCountry references a child table of CmdContinent, and CmdCity references a child table of CmdCountry.
But I get error like "wrong format" When I try to get ContinentId from cmbContinent_SelectedIndexChanged by coding.
int ContId = Convert.Into32(cmbContinent.SelectedValue.ToString());
My first two Comboboxes are like following:
private void Continent()
{
var continent = (from u in db.Continent
select new { u.ContinentName, u.ContinentId }
).ToList();
cmbContinent.DataSource = continent;
cmbContinent.DisplayMember = "ContinentName";
cmbContinent.ValueMember = "ContinentId";
}
private void cmbContinent_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbContinent.SelectedValue.ToString() != null)
{
int ContId = Convert.Into32(cmbContinent.SelectedValue.ToString()); // Here I get the error
var country = (from u in db.Country
where u.ContinentId == ContId
select new { u.CountryId, u.CountryName }).ToList();
cmbCountry.DataSource = country;
cmbCountry.DisplayMember = "CountryName";
cmbCountry.ValueMember = "CountryId";
}
}
And loading first combobox Continent in Constructor:
public NewAccount()
{
InitializeComponent();
Continent();
}
I would be appreciated if anybody could help.
I use a ComboBox in my current project and I do something similar.
int valueMax = Convert.ToInt32(cbxNumberValues.SelectedItem);
For your code here is something that should work. Note that I've done little modification to be more conventionally coded.
int contId = Convert.ToInt32(cmbContinent.SelectedItem);
I would also suggest to use 'cbx' or 'cmb' and not 'cmd' as it tend to usually say command. But that's not very important