[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1017
  • Last Modified:

Infopath and C# Code

Hi,

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

          counter++;

          // Add the item to the repeating table

          AddItem(title, status, myDate);

      }


      // Remove the first empty item from the repeating table

      DeleteFirstEmptyItem();
        }



        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", "http://www.xxxx.com/myform/2009");
            XmlNode field = doc.CreateElement("p:Field1", "http://www.xxxx.com/myform/2009");
            XmlNode node = group.AppendChild(field);
            node.InnerText = title;

            field = doc.CreateElement("p:Field2", "http://www.xxxx.com/myform/2009");
            node = group.AppendChild(field);
            node.InnerText = status;

            field = doc.CreateElement("p:Field3", "http://www.xxxx.com/myform/2009");
            node = group.AppendChild(field);
            node.InnerText = mydate.ToString();
            doc.AppendChild(group);

           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)

       itemNav.DeleteSelf();
           
    }







   
   
   
    }




}


Thanks in advance,

0
Techsavy
Asked:
Techsavy
  • 2
1 Solution
 
käµfm³d 👽Commented:
Which line throws the exception?
0
 
TechsavyAuthor Commented:
MainDataSource.CreateNavigator().SelectSingleNode("/p:myrootElement/p:Items", NamespaceManager).AppendChild(doc.DocumentElement.CreateNavigator());

0
 
käµfm³d 👽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?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now