code for updating a listbox based on choices from selection from another list box - InfoPath Dev
in

InfoPath Dev

code for updating a listbox based on choices from selection from another list box

Last post 05-19-2010 09:44 AM by Hilary Stoupa. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 05-18-2010 01:11 PM

    code for updating a listbox based on choices from selection from another list box

     I have two list boxes. one of theme is populated from an XML document. The other is to become populated from the same XML document (Customers.xml) based on what you choose in the first list box.

     I receive an "Object reference not set to an instance of an object error". The error happens in the additem function.

     This is probably easy for all of you, so I'm sorry if it sounds like a dumb question. I thought about putting it in the newbie questions, but decided to put it in code.Thanks for any and all help!

     This is customers.xml(taken from somewhere online)

    <?xml version="1.0" encoding="UTF-8"?>
    <Customers>
       <Customer>
          <CustomerID>ALFKI</CustomerID>
          <CompanyName>Alfreds Futterkiste</CompanyName>
          <ContactName>Maria Anderss</ContactName>
          <ContactTitle>Sales Representative</ContactTitle>
          <Address>Obere Str. 57</Address>
          <City>Berlin</City>
          <PostalCode>12209</PostalCode>
          <Country>Germany</Country>
          <Phone>030-0074321</Phone>
          <Fax>030-0076545</Fax>
       </Customer>
       <Customer>
          <CustomerID>ANATR</CustomerID>
          <CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
          <ContactName>Ana Trujillo</ContactName>
          <ContactTitle>Owner</ContactTitle>
          <Address>Avda. de la Constituc</Address>
          <City>Mxico D.F.</City>
          <PostalCode>05021</PostalCode>
          <Country>Mexico</Country>
          <Phone>(5) 555-4729</Phone>
          <Fax>(5) 555-3745</Fax>
       </Customer>
    </Customers>

     

     

     This is a combination of code from different sites online(also just a sample from somewhere online..i'm trying to learn something here!):

            private void AddItem(string itemId, string itemName)
            {
                XPathNavigator DOM = this.DataSources["Customers"].CreateNavigator();
                XPathNavigator group1 = DOM.SelectSingleNode("//Customers", NamespaceManager);
                XPathNavigator field1 = DOM.SelectSingleNode("/my:myFields/my:CustomerName2", NamespaceManager);
                XPathNavigator newNode = field1.Clone();
                newNode.SelectSingleNode("value").SetValue(itemId);
                //newNode.SelectSingleNode("displayname").SetValue(itemName);
                //group1.AppendChild(newNode);
            }


            public void SelectCustomer_Changed(object sender, XmlEventArgs e)
            {
                try
                {
                    // Get a reference to the Customers secondary data source,
                    // and create an XPathNavigator object for it.
                    XPathNavigator Customers =this.DataSources["Customers"].CreateNavigator();

                    // Create an XPathNavigator object positioned on the current
                    // "site", which is the SelectCustomer node in the
                    // main data source.
                    XPathNavigator myForm = e.Site.CreateNavigator();

                    // Move to the parent of the SelectCustomer node,
                    // so that the code can access the CustomerName node.
                    myForm.MoveToParent();

                    // Check if the user selected the blank value from the
                    // Drop-Down List Box after previously selecting a CustomerID.
                    if (e.Site.InnerXml == "")
                    {
                        // Clear previously selected value from CustomerName field.
                        myForm.SelectSingleNode("my:CustomerName",this.NamespaceManager).DeleteSelf();
                    }
                    else
                    {
                        // Get the Company name from the Customers data source
                        // using the value in the CustomerID node.
                        string custName = Customers.SelectSingleNode("/Customers/Customer[CustomerID = '" + e.Site.InnerXml +"']/CompanyName", this.NamespaceManager).InnerXml;

                        // Select the CustomerName node from the current row and
                        // set its value.
                       // myForm.SelectSingleNode("my:CustomerName",this.NamespaceManager).SetValue(custName);
                        AddItem(custName, custName);

                     
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message);
                }

  • 05-18-2010 01:24 PM In reply to

    Re: code for updating a listbox based on choices from selection from another list box

     Will you be using your form in a browser?  If not, you may very well be able to accomplish what you need using filtering, and no code.

    But I'm a bit confused about what you are trying to accomplish with that code.  Could you give a bit more detail about what you are trying to do?

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 05-18-2010 01:30 PM In reply to

    Re: code for updating a listbox based on choices from selection from another list box

     It is going to be used in a browser.

     

    There is one field SelectCustomer(list box), and one field CustomerName2(another list box). (there was a CustomerName field that was a text field, now I'm trying to do this with two list boxes)

    SelectCustomer is static, and just pulls data from the XML sheet to contain the list of Customers. There are only two: ALFKI and ANATR. When you click on one of those options, the other list box is two fill in with options. These options are going to be the company name based on the customer. So if I click on ALFKI in the first listbox (SelectCustomer) then the second list box will fill in with the option Alfreds Futterkiste.

    Did that make sense?

  • 05-18-2010 02:00 PM In reply to

    Re: code for updating a listbox based on choices from selection from another list box

    I think the problem is in these two lines:

        XPathNavigator group1 = DOM.SelectSingleNode("//Customers", NamespaceManager);
                XPathNavigator field1 = DOM.SelectSingleNode("/my:myFields/my:CustomerName2", NamespaceManager);

     

    I think I just have a syntax problem or something. Or the problem is with incorrectly binding or something. That isn't a concept I have figured out yet.

  • 05-18-2010 02:22 PM In reply to

    Re: code for updating a listbox based on choices from selection from another list box

    If the "Customers" group is in your main data source, I'd expect it to use the "my" namespace, like your "CustomerName2" field is. Just right click the group in the data source task pane and select "Copy XPath" and use that instead of "//Customers".

    Hilary Stoupa

  • 05-19-2010 06:07 AM In reply to

    Re: code for updating a listbox based on choices from selection from another list box

     I have no data sources listed in the data sources pane, so there must be something else that I'm doing wrong.  Should I see the XML file I'm using in that data sources pane?

  • 05-19-2010 06:26 AM In reply to

    Re: code for updating a listbox based on choices from selection from another list box

     Ah, I did find it. but it is a secondary source. does that matter?

  • 05-19-2010 09:44 AM In reply to

    Re: code for updating a listbox based on choices from selection from another list box

    Looking back at your original post: 

    XPathNavigator DOM = this.DataSources["Customers"].CreateNavigator();
    XPathNavigator group1 = DOM.SelectSingleNode("//Customers", NamespaceManager);
    XPathNavigator field1 = DOM.SelectSingleNode("/my:myFields/my:CustomerName2", NamespaceManager);

    You've declared your DOM variable using the Customers data source. So the Customers group may or may not have a namespace -- the easiest way to find out is to right click the node in the Data Source Task Pane, select Copy XPath, and paste it somewhere to see what the XPath looks like. But then, since DOM is a navigator for your secondary data source, if the XPath you are using for the field1 navigator is from your Main Data Source (looks like it is), you can't use your DOM variable to select that node. You'd need something like MainDataSource.CreateNavigator().

    Hilary Stoupa

Page 1 of 1 (8 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.