I'm kinda new here, and i have a lot questions for a program I'm making, but the one that bugs me most is the one on the title.
Basically, I have a Combobox, which I fill using this:
DataSet ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter("SELECT cveestado, nomestado FROM tbEstado", conexion);
da.Fill(ds, "FillDropDown");
cbEstado.DisplayMember = "Nomestado";
cbEstado.ValueMember = "CveEstado";
cbEstado.DataSource = ds.Tables["FillDropDown"];
conexion.Close();
And based on whatever the user selects, I want to fill another combobox based on the first selection.
So far I have this, but it does't works:
private void cbEstado_TextChanged(object sender, EventArgs e)
{
if (cbEstado.SelectedValue.ToString() == "Tabasco")
{
try
{
DataSet ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter("SELECT cvemunicipio, nommunicipio FROM tbMunicipio where cveestado = 27", conexion);
da.Fill(ds, "FillDropDown");
cbMunicipio.DisplayMember = "Nommunicipio";
cbMunicipio.ValueMember = "Cvemunicipio";
cbMunicipio.DataSource = ds.Tables["FillDropDown"];
conexion.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
Basically, if the user selects "Tabasco", I want the second combobox to fill with the municipalities of Tabasco.
My sql code is the following, just in case:
create table tbEstado
(cveEstado int not null,
nomEstado varchar(45) not null,
constraint pkcveEstado primary key (cveEstado)
)engine=innodb;
create table tbMunicipio
(cveMunicipio int not null,
nomMunicipio varchar(45) not null,
cveEstado int not null,
constraint pkcveMunicipio primary key (cveMunicipio),
constraint fkcveEstado foreign key (cveEstado) references tbEstado(cveEstado)
)engine=innodb;
Thanks!
EDIT
The answer, thanks to https://stackoverflow.com/users/1197518/steve, is:
private void cbEstado_TextChanged(object sender, EventArgs e)
{
if (cbEstado.SelectedValue != null && Convert.ToInt32(cbEstado.SelectedValue) == 27)
{
try
{
DataSet ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter("SELECT cvemunicipio, nommunicipio FROM tbMunicipio where cveestado = 27", conexion);
da.Fill(ds, "FillDropDown");
cbMunicipio.DisplayMember = "Nommunicipio";
cbMunicipio.ValueMember = "Cvemunicipio";
cbMunicipio.DataSource = ds.Tables["FillDropDown"];
conexion.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}