Infopath and C# Code

Posted on 2011-05-02
Last Modified: 2012-05-11

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,

Question by:Techsavy
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    Which line throws the exception?

    Author Comment

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

    LVL 74

    Accepted Solution

    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?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now