none
How to persist data in dynamically populated dropdown listbox

    Question

  • Hi,
     
    I have a browser-enabled InfoPath form with two dropdown listboxes.  Listbox #2 is bound to an XML file used as a secondary data source.  When a value is selected in Listbox #1, I dynamically populate Listbox #2.  That all works fine.

    My question is: how do I save the data and selection from Listbox #2 after the form has been closed and reopened?

    I tried binding Listbox #2 to a field (of the same name) from the Main data source but it wouldn't take.  There was no error message, it just wouldn't work.

    I'm new to SharePoint and InfoPath, any help you can offer would be appreciated.


    Thanks
    Wednesday, July 23, 2008 3:35 PM

Answers

  • No, there is no special way to write to the main data source compared to a secondary data source. It is all XML you are manipulating. The only difference is that you have namespaces to consider in the Main data source, which you probably did not have in your secondary data source, so you might be violating the schema of the Main data source when you try to write XML to it right now. 

    Take a look at this article; if you followed my previous instructions, you should have a repeating group in your Main data source, so can do something similar as described in the article.
    • Marked as answer by bluering Friday, July 25, 2008 4:02 PM
    Friday, July 25, 2008 2:31 AM

All replies

  • bluering said:

    Hi,
     
    I have a browser-enabled InfoPath form with two dropdown listboxes.  Listbox #2 is bound to an XML file used as a secondary data source.  When a value is selected in Listbox #1, I dynamically populate Listbox #2.  That all works fine.

    My question is: how do I save the data and selection from Listbox #2 after the form has been closed and reopened?

    I tried binding Listbox #2 to a field (of the same name) from the Main data source but it wouldn't take.  There was no error message, it just wouldn't work.

    I'm new to SharePoint and InfoPath, any help you can offer would be appreciated.


    Thanks



    When you add a drop-down list box to a form template, a field is created. When you select an item from the drop-down list box and save the form, the value of the selected item is stored in this field. Note: This field stores just one value, the selected value.

    To populate a drop-down list box, you need to either enter items manually or bind the drop-down list box to a secondary data source. You can also use a repeating group in the Main data source of the form as the data source for the drop-down.

    Any data that you store in the Main data source of a form will be available when you save and open the form again. This is not true for secondary data sources. So if you bound your drop-down list box to an XML file, and populated this XML file with entries, those entries would not be present if you saved and re-opened the form, because the XML file is a secondary data source. So in your case, you need to create a repeating group in the Main data source of the form, and populate the fields in this group with the entries for the drop-down list box.

    The quickest way to create a repeating group is to drag-and-drop a repeating table with 2 columns to the form template, rename the first field to "Value", rename the second field to "DisplayName", delete the repeating table from the View (it will continue to exist in the Data Source), and then bind the drop-down list box to the repeating group you just created.

    Also read this article if you have not already seen it.
    Thursday, July 24, 2008 9:06 AM
  • Thanks for the direction.

    I followed your advice but still have trouble writing data to the new repeating group in the Main data source.

    I read the article you suggested some time ago but it doesn't describe how to write to the Main data source, it only shows how to write to a secondary data source.

    Here's my code:

    private void LoadSubTypes(System.Collections.SortedList list)
    {
    // get a reference to the data source
    XPathNavigator DOM = MainDataSource.CreateNavigator();

    // delete all child nodes ("option") from the data source
    DOM.MoveToChild("subtypes", "");

    while (DOM.MoveToChild("subtype", "")) { DOM.DeleteSelf(); }

    // for each item, insert new child nodes ("option") into the data source,
    // the new child nodes contain "Title" and "ID" data values.
    int c = list.Count;
    for (int i = 0; i < c; ++i)
    {
        XmlWriter xw = DOM.AppendChild();
        xw.WriteStartElement("subtype");
        xw.WriteStartElement(
    "value");
        xw.WriteValue((
    string)list.GetByIndex(i)); // Title
        xw.WriteEndElement();
        xw.WriteStartElement(
    "displayname");
        xw.WriteValue((
    string)list.GetKey(i)); // ID
        xw.WriteEndElement();
        xw.WriteEndElement();
        xw.Close();
    }
    }

    This code worked when I was writing to the secondary data source (XML file) but now when I run the code I get this XML exception on the last statement, xw.Close():

    Unexpected XML declaration.  The XML declaration must be the first node in the document and no white space characters are allowed to appear before it.  Line 1, position 9.

    Is there a special way to write to the Main data source, unlike writing to a secondary data source?

    Thanks.


    Thursday, July 24, 2008 7:21 PM
  • No, there is no special way to write to the main data source compared to a secondary data source. It is all XML you are manipulating. The only difference is that you have namespaces to consider in the Main data source, which you probably did not have in your secondary data source, so you might be violating the schema of the Main data source when you try to write XML to it right now. 

    Take a look at this article; if you followed my previous instructions, you should have a repeating group in your Main data source, so can do something similar as described in the article.
    • Marked as answer by bluering Friday, July 25, 2008 4:02 PM
    Friday, July 25, 2008 2:31 AM
  • SYM Wong-A-Ton,

    Thanks so much!!!  That's was the answer.


    Friday, July 25, 2008 4:12 PM