4

im making a wpf wherein one combobox populates depending on another combobox. however, only one combobox populated.

this is my code below.

 public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
            this.Load += Form4_Load;
        }

        string connstring = ("Server=localhost;Port=5432;User Id=postgres;Password=021393;Database=postgres;");

        private void Form4_Load(object sender, EventArgs e)
        {
            string query = "SELECT * FROM data_organsystem";            
            fillCombo(comboBox3, query, "name", "id");
            comboBox3_SelectedIndexChanged(null, null);
        }

        private void fillCombo(ComboBox combo, string query, string displayMember, string valueMember)
        {
            NpgsqlConnection conn = new NpgsqlConnection(connstring);
            NpgsqlCommand cmd = new NpgsqlCommand(query, conn);
            NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            combo.DataSource = dt;             
            combo.DisplayMember = displayMember;            
            combo.ValueMember = valueMember;
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            int val;
            Int32.TryParse(comboBox3.SelectedValue.ToString(), out val);
            string query = "SELECT * FROM data_symptom WHERE organ_system_id = " + val;
            fillCombo(comboBox4, query, "name", "id");
        }
    }
}

if you have any idea on how to edit this code, it would be a big help. thanks!

FishySwede
  • 1,563
  • 1
  • 17
  • 24
newbie
  • 201
  • 2
  • 4
  • 12
  • are you getting any error? – sujith karivelil Aug 19 '15 at 06:51
  • actually no. the code runs. but only one combobox populates – newbie Aug 19 '15 at 06:52
  • Are you sure your query returns something? – Shaharyar Aug 19 '15 at 07:33
  • I affraid `comboBox3.SelectedValue.ToString()` will throw exception because `comboBox3.SelectedValue` is `null`. At least in the first executing of `comboBox3_SelectedIndexChanged` method. `ComboBox.SelectedValue` return `null` if nothing selected. And maybe you can use `ComboBox.SelectedValueChanged` event – Fabio Aug 19 '15 at 07:37
  • yes. do you have another idea @Shaharyar on how it works? – newbie Aug 19 '15 at 07:37
  • You need to put a break point at `Int32.TryParse(..` to see what is actually the value coming from it. Is it even a successful conversion or not. Also get the `query` generated by code and execute it in database to see if it returns something or not – Shaharyar Aug 19 '15 at 07:39

2 Answers2

1

You will get error when you executing method comboBox3_SelectedIndexChanged àt the line

Int32.TryParse(comboBox3.SelectedValue.ToString(), out val);

Because comboBox3.SelectedValue is null if no item selected in the ComboBox, I didn't see in your code that you selected some items before calling comboBox3_SelectedIndexChanged first time.

Because method comboBox3_SelectedIndexChanged executed inside Form.Load eventhandler exception wasn't shown. Check this: https://stackoverflow.com/a/3209813/1565525.
That is why you didn't get any errors

You need to check SelectedValue for null before using it

If(this.comboBox3.SelectedValue is null)
{
    this.comboBox4.DataSource = null; //Remove all items if nothing selected
}
else
{
    Int32 val= (Int32)this.ComboBox3.SelectedValue;
    string query = "SELECT id, name FROM data_symptom WHERE organ_system_id = " + val;
    fillCombo(this.comboBox4, query, "name", "id");
 }

Because you using DataBinding when filling ComboBox with items it will be logically to use SelectedValueChanged event handler

private void comboBox3_SelectedValueChanged(object sender, EventsArgs e)
{
    //same code
}
Community
  • 1
  • 1
Fabio
  • 31,528
  • 4
  • 33
  • 72
0

You never add comboBox4 to your form. comboBox3 is added via constructor but comboBox4 is created and added to nowhere.

Berk Kurkcuoglu
  • 1,453
  • 1
  • 9
  • 11