1

I have been trying to populate some values from an xml file in two separate Combobox. The contents of the second combobox will depend on the selection of the first combo box. The first combobox will contain the "ClusterName" & the second will contain the corresponding "MachineID"

I have populated the clustername combo box but I am unable to populate the corresponding machineid.

The C# code I used to populate the the first combobox:

public void PopulateClusterNameDropDown()
{
    XDocument doc = XDocument.Load("the path of the file");
    List<string> clusterNameList = doc.Root
        .Elements("Machines")
        .Elements("Cluster")
        .Elements("ClusterName")
        .Select(x => (string)x)
        .ToList();

    BindingSource bs = new BindingSource();
    bs.DataSource = clusterNameList;
    cbSelectCluster.DataSource = bs;
}

Edits:

The code I tried to populate the Machineid Combo box

private void cbSelectCluster_SelectedIndexChanged(object sender, EventArgs e)
{
    XElement root = XElement.Load("file path");
    if (!cbSelectCluster.Text.Trim().Equals("")) {
        cbSelectMachineID.Enabled = true;
        cbSelectMachineID.Items.Clear();

        var selected = from cli in root.Elements("Machines").Elements("Cluster").Elements("MachineID")
        where cli.Element("ClusterName").Value.Equals(cbSelectCluster.Text)
        select cli;

        BindingSource bs = new BindingSource();
        bs.DataSource = selected;
        cbSelectMachineID.DataSource = bs;
    }
}

The Xml is as follows

<Config>
    <Machines>
        <Cluster>
            <ClusterName>ABC</ClusterName>
            <MachineID>Machine123</MachineID>
            <MachineID>Machine456</MachineID>
            <MachineID>Machine789</MachineID>

        </Cluster>
        <Cluster>
            <ClusterName>XYZ</ClusterName>
            <MachineID>Machine111</MachineID>
            <MachineID>Machine222</MachineID>
            <MachineID>Machine333</MachineID>
        </Cluster>
    </Machines>
</Config>
subhrendu
  • 147
  • 1
  • 2
  • 19
  • 1
    Please update the issue you are facing along with the code you tried – Vidya Jan 22 '16 at 13:06
  • 1
    Is there any error code? – Ian Jan 22 '16 at 13:18
  • @Ian The error code I am getting is A first chance exception of type 'System.NullReferenceException' occurred in myapplication.exe Additional information: Object reference not set to an instance of an object. – subhrendu Jan 22 '16 at 13:23
  • @subhrendu ah, that hints a lot! ;) and where does the error occur? which line? – Ian Jan 22 '16 at 13:24
  • 2
    Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – mybirthname Jan 22 '16 at 13:27
  • I am getting that exception at "cli.Element("ClusterName").Value.Equals(where cli.Element("ClusterName").Value.Equals(cbSelectCluster.Text)" – subhrendu Jan 22 '16 at 13:29
  • 1
    Then you know where the errors could be. ;) prime suspect: `cli.Element("ClusterName")` is not there... – Ian Jan 22 '16 at 13:31
  • okay maybe I am trying the wrong way. Can any one of you guys help me fix the problem I am having regarding populating the combo box? – subhrendu Jan 22 '16 at 15:40

1 Answers1

1

You are getting this issue because you are looking for ClusterName element under MachineID element. You just need to fix the LINQ.

Please find the fixed code below.

Also, it might be worth noting that I have removed the line cbSelectMachineID.Items.Clear(), since you cannot clear a combobox if you set the data-source.

private void cbSelectCluster_SelectedIndexChanged(object sender, EventArgs e)
{
    XElement root = XElement.Load("filename.xml");
    if (!cbSelectCluster.Text.Trim().Equals(""))
    {
        cbSelectMachineID.Enabled = true;

        var machineIds = root
            .Elements("Machines")
            .Elements("Cluster")
            .Where(clusterElement => (string)clusterElement.Element("ClusterName") == cbSelectCluster.Text)
            .Elements("MachineID")
            .Select(x => (string)x)
            .ToList();

        BindingSource bs = new BindingSource();
        bs.DataSource = machineIds;
        cbSelectMachineID.DataSource = bs;
    }
}

Hope this helps.

frostedcoder
  • 153
  • 7
  • Thanks man it worked :) . I knew I had to tweak the linq, unfortunately just a begginner in this field. Will have to dig deeper ;) – subhrendu Jan 25 '16 at 07:01