1

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);
                }
            }
        }
Community
  • 1
  • 1
John Smith
  • 47
  • 2
  • 6
  • And what is your problem? – Steve May 16 '13 at 17:05
  • Hi @Steve! I want to fill my second combobox (cbMunicipio) based on what I choose in first combobox (cbEstado). – John Smith May 16 '13 at 17:06
  • 1
    I discourage the use of `System.Data` in WPF client-side. Create a proper data model instead. And please move your DB access logic out of the code behind. And don't manipulate UI elements in code in WPF. Create a proper ViewModel and use DataBinding. – Federico Berasategui May 16 '13 at 17:07
  • Is 'Tabasco' a value from the column `Cveestado`? – Steve May 16 '13 at 17:09
  • Ahm, as much as I appreciate your help, @HighCore, I didn-t understand a little bit. I-ll research more on what you-re saying. Thanks. – John Smith May 16 '13 at 17:09
  • There are a few examples of this you might also find useful: http://stackoverflow.com/questions/3469287/populate-one-combobox-based-on-selection-of-another and http://stackoverflow.com/questions/14756752/one-combo-box-filtering-another-combo-box-in-a-wpf-datagrid and http://stackoverflow.com/questions/6874376/populate-combobox-based-on-another-combobox-using-xaml and http://stackoverflow.com/questions/14651098/databinding-2-comboboxes-issue – dash May 16 '13 at 17:11
  • No, CveMunicipio is the ID of the municipality, Tabasco has a value of 27 in CveEstado, @Steve – John Smith May 16 '13 at 17:11
  • @JohnSmith by `Data Model` I mean, if your data represents a State or City, create a `City` class and put the relevant properties (such as Name, Population, State, Etc) in that class, instead of using `DataTable`, `DataRow` and all that. You may then want to use an [ORM](http://en.wikipedia.org/wiki/Object-relational_mapping) to map your database to your classes and objects – Federico Berasategui May 16 '13 at 17:12
  • Thanks, @dash, I-ll check them. – John Smith May 16 '13 at 17:12
  • And this is the error. You check the SelectedValue property that should contains the number 27 not the string 'Tabasco' – Steve May 16 '13 at 17:12
  • By moving your data access code outside the code-behind I'm talking about [Separation of Concerns](http://en.wikipedia.org/wiki/Separation_of_concerns). Each significant part of the application (the Model, the DataBase Access, The services, The UI) should have a separate and defined place (maybe a separate project in Visual Studio). If you mash everything together then it's all a mess and spaghetti code is rampant all over the place and it's impossible to debug or maintain. – Federico Berasategui May 16 '13 at 17:15
  • And by using DataBinding and not manipulating UI elements in code I'm talking about [MVVM](http://stackoverflow.com/questions/14381402/wpf-programming-methodology/14382137#14382137) which is the preferred approach to WPF. – Federico Berasategui May 16 '13 at 17:16
  • Thank you for your concern, @HighCore. I'm still a noob doing code, and I frankencode a lot, but it works. Next semester in school I'm going to learn a lot more about how to code well. – John Smith May 16 '13 at 17:18

2 Answers2

0

You are checking the SelectedValue property that is linked to the column CveEstado.

This column contains numbers not the string Tabasco

    if (cbEstado.SelectedValue != null && Convert.ToInt32(cbEstado.SelectedValue) == 27)
    {
        try
        {
        .......

This will probably solve your actual problem, but please check also the advices given to you in the comments to your question

Steve
  • 213,761
  • 22
  • 232
  • 286
0

You can use dataview for example:

cbEstado.Datasource =ds.Tables["State"];
cbMunicipio.Datasource = new Dataview(ds.Tables["Muncipalities"];);

on cbSample1_SelectedIndexChange event you can:

(cbMunicipio.DataSource as DataView).RowFilter = "cveEstado='" +  cbEstado.SelectedValue.ToString() + "'";
jersoft
  • 478
  • 2
  • 9
  • 20