Infopath and C# Code


I am using Infopath and what I am trying to do is :  I have a repeating table that should get its data from a secondary data source. In this case, the secondary data source is a SharePoint List.

But, we cannot retrieve multiple items from a secondary data source and copy them into a repeating table in Main data Source.

So I am trying to achieve this through C# code. I am getting an "Invalid Operation Exception" .  Can anyone help, where I am doing wrong?

public void InternalStartup()
            ((ButtonEvent)EventManager.ControlEvents["get_Items"]).Clicked += new ClickedEventHandler(get_Items_Clicked);

        public void get_Items_Clicked(object sender, ClickedEventArgs e)
            // Write your code here.
            XPathNavigator secDSNav = DataSources["mydatasource"].CreateNavigator();
            //Retrieve the rows of the secondary data sources.

      XPathNodeIterator rows = secDSNav.Select("/dfs:myFields/dfs:dataFields/dfs:Items",NamespaceManager);

      int counter = 1;

      while (rows.MoveNext())
          string title = GetValueOrEmptyString(rows.Current.SelectSingleNode("@Field1", NamespaceManager));

          string status = GetValueOrEmptyString(rows.Current.SelectSingleNode("@Field2", NamespaceManager));

          DateTime myDate = GetValueOrEmptyString(rows.Current.SelectSingleNode("@Field3", NamespaceManager));

          // Increment the counter


          // Add the item to the repeating table

          AddItem(title, status, myDate);


      // Remove the first empty item from the repeating table


        public static string GetValueOrEmptyString(XPathNavigator node)
            return node == null ? string.Empty : node.Value;

        private void AddItem(string title, string status, DateTime mydate)

            XmlDocument doc = new XmlDocument();
            XmlNode group = doc.CreateElement("p:Item", "");
            XmlNode field = doc.CreateElement("p:Field1", "");
            XmlNode node = group.AppendChild(field);
            node.InnerText = title;

            field = doc.CreateElement("p:Field2", "");
            node = group.AppendChild(field);
            node.InnerText = status;

            field = doc.CreateElement("p:Field3", "");
            node = group.AppendChild(field);
            node.InnerText = mydate.ToString();

           MainDataSource.CreateNavigator().SelectSingleNode("/p:myrootElement/p:Items", NamespaceManager).AppendChild(doc.DocumentElement.CreateNavigator());


        private void DeleteFirstEmptyItem()


        XPathNavigator domNav = MainDataSource.CreateNavigator();

        XPathNavigator itemNav = domNav.SelectSingleNode("/p:myrootElement/p:Items/p:Item[1]", NamespaceManager);

        if (itemNav != null)




Thanks in advance,

Who is Participating?
käµfm³d 👽Connect With a Mentor Commented:
One thing I'd suggest is that you don't need to call CreateNavigator on "doc"--you should just be able to append "doc".

MainDataSource.CreateNavigator().SelectSingleNode("/p:myrootElement/p:Items", NamespaceManager).AppendChild(doc);

Open in new window

What is the text of the exception's message?
käµfm³d 👽Commented:
Which line throws the exception?
TechsavyAuthor Commented:
MainDataSource.CreateNavigator().SelectSingleNode("/p:myrootElement/p:Items", NamespaceManager).AppendChild(doc.DocumentElement.CreateNavigator());

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.