<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://www.infopathdev.com:443/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Code</title><link>https://www.infopathdev.com:443/forums/45.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2007 SP3 (Build: 31118.962)</generator><item><title>Re: Creating New Row Entries in Code</title><link>https://www.infopathdev.com:443/forums/thread/15223.aspx</link><pubDate>Tue, 03 Apr 2007 18:12:19 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:15223</guid><dc:creator>BhailDa</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/15223.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=45&amp;PostID=15223</wfw:commentRss><description>And my name isn't Sharon !</description></item><item><title>Re: Creating New Row Entries in Code</title><link>https://www.infopathdev.com:443/forums/thread/15222.aspx</link><pubDate>Tue, 03 Apr 2007 18:11:09 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:15222</guid><dc:creator>BhailDa</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/15222.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=45&amp;PostID=15222</wfw:commentRss><description>Should have said I'm using InfoPath 2007. &lt;br /&gt;Anyway after countless attempts today I've semi-worked out the issue. It comes down to how the underlying schema is defined. If for instance I have a 'header' and 'items', with a 1-to-many relationship, I would need intermediate XML Group setup as optional. &lt;br /&gt;&lt;br /&gt;Header 0 -n Items&lt;br /&gt;Instead have:&lt;br /&gt;Header 0-1 ItemGroup 0-n Items&lt;br /&gt;&lt;br /&gt;This intermediate group would create, when dragging the fields from the datasource to the InfoPath Manifest, another optional section. The optional section, within properties ,this would have the XML ID that can be referred to within the code. Else you'll end up with a repeatable table and its container section without and XML I and hence wont be able to create the InnerXML. Wierd eh !!!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;u&gt;Code:&lt;/u&gt;&lt;br /&gt;&lt;br /&gt;using Microsoft.Office.InfoPath;&lt;br /&gt;using System;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;using System.Xml;&lt;br /&gt;using System.Xml.XPath;&lt;br /&gt;using mshtml;&lt;br /&gt;&lt;br /&gt;namespace GR&lt;br /&gt;{&lt;br /&gt;    public partial class FormCode&lt;br /&gt;    {&lt;br /&gt;        // Member variables are not supported in browser-enabled forms.&lt;br /&gt;        // Instead, write and read these values from the FormState&lt;br /&gt;        // dictionary using code such as the following:&lt;br /&gt;        //&lt;br /&gt;        // private object _memberVariable&lt;br /&gt;        // {&lt;br /&gt;        //     get&lt;br /&gt;        //     {&lt;br /&gt;        //         return FormState["_memberVariable"];&lt;br /&gt;        //     }&lt;br /&gt;        //     set&lt;br /&gt;        //     {&lt;br /&gt;        //         FormState["_memberVariable"] = value;&lt;br /&gt;        //     }&lt;br /&gt;        // }&lt;br /&gt;&lt;br /&gt;        // NOTE: The following procedure is required by Microsoft Office InfoPath.&lt;br /&gt;        // It can be modified using Microsoft Office InfoPath.&lt;br /&gt;        public void InternalStartup()&lt;br /&gt;        {&lt;br /&gt;            ((ButtonEvent)EventManager.ControlEvents["btSave"]).Clicked += new ClickedEventHandler(btSave_Clicked);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        #region Utils&lt;br /&gt;        #region Check Nodes&lt;br /&gt;        public void DeleteNil(XPathNavigator node)&lt;br /&gt;        {&lt;br /&gt;            if (node.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance"))&lt;br /&gt;                node.DeleteSelf();&lt;br /&gt;        }&lt;br /&gt;        #endregion&lt;br /&gt;        #region DateFormat&lt;br /&gt;        private string formatPeriod(int Period)&lt;br /&gt;        {&lt;br /&gt;            string sPeriodFormat = string.Empty;&lt;br /&gt;            if (Period &amp;lt; 10)&lt;br /&gt;            {&lt;br /&gt;                sPeriodFormat = string.Format("0{0}", Period);&lt;br /&gt;            }&lt;br /&gt;            else&lt;br /&gt;            {&lt;br /&gt;                sPeriodFormat = string.Format("{0}", Period);&lt;br /&gt;            }&lt;br /&gt;            return sPeriodFormat;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        private string formatDate(DateTime Date, bool JustDate)&lt;br /&gt;        {&lt;br /&gt;            // The date format must including preceeding zeros where less than 10.&lt;br /&gt;            // if the datatype defined as dateTime then must include the time as well.&lt;br /&gt;            if (JustDate)&lt;br /&gt;            {&lt;br /&gt;                return string.Format("{0}-{1}-{2}", Date.Year, formatPeriod(Date.Month), formatPeriod(Date.Day));&lt;br /&gt;            }&lt;br /&gt;            else&lt;br /&gt;            {&lt;br /&gt;                // Includes Time format&lt;br /&gt;                return string.Format("{0}-{1}-{2}T{3}:{4}:{5}", Date.Year, formatPeriod(Date.Month), formatPeriod(Date.Day), formatPeriod(Date.Hour), formatPeriod(Date.Minute), formatPeriod(Date.Second));&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        #endregion&lt;br /&gt;        #endregion&lt;br /&gt;&lt;br /&gt;        public void btSave_Clicked(object sender, ClickedEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            // Save Changes&lt;br /&gt;            XPathNavigator xn = MainDataSource.CreateNavigator();&lt;br /&gt;            &lt;br /&gt;            // Loop through the Items&lt;br /&gt;            XPathNodeIterator myXPathNodeIterator = xn.Select("/myFields/ItemGroup/Item", this.NamespaceManager);&lt;br /&gt;            // Loop through each item and create a goods receipt.&lt;br /&gt;            while (myXPathNodeIterator.MoveNext())&lt;br /&gt;            {&lt;br /&gt;                XPathNavigator navCurrent = myXPathNodeIterator.Current;&lt;br /&gt;                XPathNavigator xItem = navCurrent.SelectSingleNode("Qty", this.NamespaceManager);&lt;br /&gt;                string sQty = xItem.Value;&lt;br /&gt;&lt;br /&gt;                // Find the container group and create a new Goods Receipt Row&lt;br /&gt;                XPathNavigator xGroup = navCurrent.SelectSingleNode("GroupGR", this.NamespaceManager);&lt;br /&gt;                AddNewRow(ref xGroup);&lt;br /&gt;&lt;br /&gt;                // Now reference newly created row&lt;br /&gt;                XPathNavigator xnNewRow = navCurrent.SelectSingleNode("GroupGR/GR[last()]", this.NamespaceManager);&lt;br /&gt;                xnNewRow.SelectSingleNode("GRQty", this.NamespaceManager).SetValue(sQty);&lt;br /&gt;                &lt;br /&gt;                // Delete empty GR&lt;br /&gt;                deleteEmptyGR(ref navCurrent);&lt;br /&gt;            }&lt;br /&gt;           &lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        private void deleteEmptyGR(ref XPathNavigator navCurrent)&lt;br /&gt;        {&lt;br /&gt;            // Issue with some of the Goods Receipt Rows being populated without rows&lt;br /&gt;            // Simply loop through them and delete accordingly&lt;br /&gt;            int iDeletionIndex = 0;&lt;br /&gt;            bool bEmptyRecordFound = false;&lt;br /&gt;&lt;br /&gt;            XPathNodeIterator xGR_Iterator = navCurrent.Select("GroupGR/GR", this.NamespaceManager);&lt;br /&gt;            while (xGR_Iterator.MoveNext())&lt;br /&gt;            {&lt;br /&gt;                iDeletionIndex++;&lt;br /&gt;                XPathNavigator xGRCurrent = xGR_Iterator.Current;&lt;br /&gt;                XPathNavigator xGRQty = xGRCurrent.SelectSingleNode("GRQty", this.NamespaceManager);&lt;br /&gt;                if (xGRQty.Value == null || xGRQty.Value == "" || xGRQty.Value == string.Empty)&lt;br /&gt;                {&lt;br /&gt;                    // Assuming only one record found&lt;br /&gt;                    bEmptyRecordFound = true;&lt;br /&gt;                    break;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;            if (bEmptyRecordFound)&lt;br /&gt;            {&lt;br /&gt;                XPathNavigator xDelete = navCurrent.SelectSingleNode("GroupGR/GR[" + iDeletionIndex + "]", this.NamespaceManager);&lt;br /&gt;                xDelete.DeleteSelf();&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public void AddNewRow(ref XPathNavigator docXN)&lt;br /&gt;        {&lt;br /&gt;            // Select the Container Node&lt;br /&gt;            XPathNavigator xnNode = this.Template.Manifest.SelectSingleNode("//xsf:xDocumentClass/xsf:views/xsf:view[@name='Entry']/xsf:editing/xsf:xmlToEdit[@name='GroupGR_2']/xsf:editWith/xsf:fragmentToInsert/xsf:chooseFragment/GroupGR", this.NamespaceManager);&lt;br /&gt;&lt;br /&gt;            //Append the node from the Manifest file to the main DOM&lt;br /&gt;            docXN.AppendChild(xnNode.InnerXml);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;</description></item><item><title>Re: Creating New Row Entries in Code</title><link>https://www.infopathdev.com:443/forums/thread/15221.aspx</link><pubDate>Tue, 03 Apr 2007 16:15:09 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:15221</guid><dc:creator>Matt Faus</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/15221.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=45&amp;PostID=15221</wfw:commentRss><description>Hi Sharon,&lt;br /&gt;&lt;br /&gt;I'm sorry to hear about your troubles with managed code - have you been through the Resources section of our website? There are several tutorials and articles there that might help you.&lt;br /&gt;&lt;br /&gt;As for creating a clone of a node that does not exist - try cloning it from the sampledata.xml file inherit in every form.  Here's a code snippet that explains what I'm talking about:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;IXMLDOMDocument2 SampleDataDom;&lt;br /&gt;SampleDataDom = (IXMLDOMDocument2)thisXDocument.CreateDOM();&lt;br /&gt;SampleDataDom.async = false;&lt;br /&gt;SampleDataDom.validateOnParse = false;&lt;br /&gt;SampleDataDom.setProperty("SelectionNamespaces", "_all_form_namespaces_");&lt;br /&gt;SampleDataDom.load("sampledata.xml");&lt;br /&gt;&lt;br /&gt;IXMLDOMNode nClone = SampleDataDom.DOM.selectSingleNode( "/my:myFields/my:field1" ).cloneNode(true);&lt;/pre&gt;&lt;br /&gt;</description></item><item><title>Creating New Row Entries in Code</title><link>https://www.infopathdev.com:443/forums/thread/13103.aspx</link><pubDate>Tue, 03 Apr 2007 12:34:27 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:13103</guid><dc:creator>BhailDa</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/13103.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=45&amp;PostID=13103</wfw:commentRss><description>I have to admit I'm having a very difficult time with this managed code business. I have to say its very user-unfriendly and the samples and help available are limited to say the least.&lt;br /&gt;&lt;br /&gt;I want to programmatically enter new rows within "an empty" table. The table may only be visible within a restricted view. This is next to impossible as XPathNavigator hasn't got a reference to the new nodes...because they dont exist ! The only way to create them is to refer to the InfoPath Manifest to constrcut the appropriate InnerXML and append this as a child. This is hit and miss as it depends on a host of screen settings.&lt;br /&gt;&lt;br /&gt;There must be an easier way to do this. Thoughts/views?</description></item></channel></rss>