Modify Boolean Value Programatically - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

Modify Boolean Value Programatically

Last post 05-22-2009 12:38 PM by Hilary Stoupa. 11 replies.
Page 1 of 1 (12 items)
Sort Posts: Previous Next
  • 05-22-2009 09:10 AM

    Modify Boolean Value Programatically

    I have an InfoPath form that has sections that are either hidden or not hidden based upon a boolean variable that I created called "ShowInput". I want to modify that variable from "TRUE" to "FALSE" as part of my custom Submit code. I am writing this in C#, how would I do this?

  • 05-22-2009 09:43 AM In reply to

    Re: Modify Boolean Value Programatically

    Hi RRaseley and welcome,

    You can achieve this with the following piece of code:

    XPathNavigator navigator = this.MainDataSource.CreateNavigator();

     

    if(Some Condition == true) 

    {

       navigator.selectSingleNode("/my:myFields/my:ShowInput").InnerXml = "True";

    }

    else

    {

       navigator.selectSingleNode("/my:myFields/my:ShowInput").InnerXml = "False";

    }

     

  • 05-22-2009 09:56 AM In reply to

    Re: Modify Boolean Value Programatically

    Thank you for the input.

    On this line:

    navigator.SelectSingleNode("/my:myFields/my:showInput").InnerXml = "False";

    I get the error:

    XPathException was unhandled by user code

    Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.

    Any guidance as far as what this means? Thanks.

  • 05-22-2009 10:08 AM In reply to

    Re: Modify Boolean Value Programatically

    This is my code:

    XPathNavigator showInputControl = MainDataSource.CreateNavigator();if (showInputControl != null)

    {

    XPathNavigator showInput = showInputControl.SelectSingleNode("/my:myFields/my:showInput", NamespaceManager);

    if (showInput != null)

    {

    showInputControl.Select(
    "/my:myFields/my:showInput").InnerXml = "False";

    }

    else

    {

    showInputControl.Select(
    "/my:myFields/my:showInput").InnerXml = "True";

    }

    }

    Which returns two errors:

    Error 1 'System.Xml.XPath.XPathNodeIterator' does not contain a definition for 'InnerXml' C:\Users\[REDACTED]\Documents\InfoPath Projects\NDA_Generic\FormCode.cs 48 74 NDA_Generic
    Error 2 'System.Xml.XPath.XPathNodeIterator' does not contain a definition for 'InnerXml' C:\Users\[REDACTED]\Documents\InfoPath Projects\NDA_Generic\FormCode.cs 53 74 NDA_Generic

  • 05-22-2009 10:17 AM In reply to

    Re: Modify Boolean Value Programatically

    Your second if statement is simply verifying the existance of the showInput node. Is that what you want? Or do you want to know if the node is blank? As far as setting values in the node, try:

    XPathNavigator showInputControl = MainDataSource.CreateNavigator();

    if (showInputControl != null)

    {

      XPathNavigator showInput = showInputControl.SelectSingleNode("/my:myFields/my:showInput", NamespaceManager);    if (showInput != null)

        {

           showInput.SetValue(
    "False");

        }

        else

        {

          showInput.SetValue("True");

        }

    }

    If your boolean can be blank, and if it is blank, you'll also have to remove the xsi:nil attribute prior to setting the value....

    Hilary Stoupa

  • 05-22-2009 10:38 AM In reply to

    Re: Modify Boolean Value Programatically

    Perhaps if I explain what I am trying to do a little better this will make more sense.

    I have an InfoPath form that I want to have two sections. The first section contains text boxes that a user can input a name and address. The second section contains text and Expression Boxes that take the name and address and put them in certain sections of the text.

    By default, only the first section is shown. I accomplish this by creating a new Data Source named "showInput" with a default value of "TRUE". I set the conditional formatting on the first section to be hidden when the value of "showInput" is "FALSE". The second section is hidden by default and is shown when the value of "showInput" is changed to "FALSE". I then use an export function to drop the current view into a PDF file.

    It should work as follows:

    1. User opens InfoPath form.
    2. User only sees boxes to fill out information
    3. User fills out information and presses the "Submit" button
    4. The Submit button has custom code that changes the value of "showInput" from "TRUE" (the default) to "FALSE"
    5. This change in value *should* make the input fields (contained in the first section) dissapear and the text with the values from the first section appear
    6. The current view (which now sould only be the text) should be exported to a PDF

    I have everything working except for the first section doesn't hide itself when the "showInput" value is set to "FALSE" even though I have the conditional formatting set to do so. Any ideas? My submit code is as follows:

    using Microsoft.Office.InfoPath;

    using System;

    using System.Windows.Forms;

    using System.Xml;

    using System.Xml.XPath;

    using mshtml;

    namespace NDA_Generic

    {

    public partial class FormCode

    {

    // Member variables are not supported in browser-enabled forms.

    // Instead, write and read these values from the FormState

    // dictionary using code such as the following:

    //

    // private object _memberVariable

    // {

    // get

    // {

    // return FormState["_memberVariable"];

    // }

    // set

    // {

    // FormState["_memberVariable"] = value;

    // }

    // }

    // NOTE: The following procedure is required by Microsoft Office InfoPath.

    // It can be modified using Microsoft Office InfoPath.

    public void InternalStartup()

    {

    EventManager.FormEvents.Submit +=
    new SubmitEventHandler(FormEvents_Submit);

    }

    public void FormEvents_Submit(object sender, SubmitEventArgs e)

    {

    e.CancelableArgs.Cancel =
    false;

    string fileName;

    XPathNavigator showInputControl = MainDataSource.CreateNavigator();if (showInputControl != null)

    {

    XPathNavigator showInput = showInputControl.SelectSingleNode("/my:myFields/my:showInput", NamespaceManager); if (showInput != null)

    {

    showInput.SetValue(
    "False");

    }

    else

    {

    showInput.SetValue(
    "True");

    }

    }

    XPathNavigator nameNode = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:textName", NamespaceManager);if (nameNode != null && !String.IsNullOrEmpty(nameNode.Value))

    {

    fileName = nameNode.Value + ".pdf";

    this.CurrentView.Export(@"C:\Users\Richard\Desktop\" + fileName, ExportFormat.Pdf);

    }

    else

    {

    e.CancelableArgs.Cancel =
    true;

    }

    }

    }

    }

  • 05-22-2009 11:08 AM In reply to

    Re: Modify Boolean Value Programatically

    Sometimes a sample simplifies things. I've attached a zip with code and a form -- right click the link in the heading of the reply and select save as to save locally. Open the xsn in design mode --you'll need to repoint it to the source code folder under Tools, Form Options, Programming....

    Hilary Stoupa

  • 05-22-2009 11:23 AM In reply to

    Re: Modify Boolean Value Programatically

    Thank you for the example and for taking the time to help me out.

    I reviewed your example and implimented the differences in code into my script. Now the showing / hiding works great, but it doesn't seem to execute the section of code that comes after (the exporting of the current view to PDF). Would something in the code that you just posted as an example cause it to not execute the next function or skip to the end?

    My Submit code is as follows:

    using Microsoft.Office.InfoPath;

    using System;

    using System.Windows.Forms;

    using System.Xml;

    using System.Xml.XPath;

    using mshtml;

    namespace NDA_Generic

    {

    public partial class FormCode

    {

    // Member variables are not supported in browser-enabled forms.

    // Instead, write and read these values from the FormState

    // dictionary using code such as the following:

    //

    // private object _memberVariable

    // {

    // get

    // {

    // return FormState["_memberVariable"];

    // }

    // set

    // {

    // FormState["_memberVariable"] = value;

    // }

    // }

    // NOTE: The following procedure is required by Microsoft Office InfoPath.

    // It can be modified using Microsoft Office InfoPath.

    public void InternalStartup()

    {

    EventManager.FormEvents.Submit +=
    new SubmitEventHandler(FormEvents_Submit);

    }

    public void FormEvents_Submit(object sender, SubmitEventArgs e)

    {

    // ??

    e.CancelableArgs.Cancel = false;

    // Set Variable for PDF Export

    string fileName;

    // Change "showInput" field from TRUE to FALSE

    XPathNavigator showInput = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:showInput", this.NamespaceManager);if (null != showInput)

    {

    if (showInput.ValueAsBoolean == true)

    {

    showInput.SetValue("false");

    return;

    }

    if (showInput.ValueAsBoolean == false)

    {

    showInput.SetValue(
    "true");return;

    }

    }

    // Export Current View to PDF

    XPathNavigator nameNode = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:textName", NamespaceManager);if (nameNode != null && !String.IsNullOrEmpty(nameNode.Value))

    {

    fileName = nameNode.Value + ".pdf";

    this.CurrentView.Export(@"C:\Users\Richard\Desktop\" + fileName, ExportFormat.Pdf);

    }

    else

    {

    e.CancelableArgs.Cancel =
    true;

    }

    }

    }

    }

  • 05-22-2009 11:34 AM In reply to

    Re: Modify Boolean Value Programatically

    I actually got it working how I imagined it...

    I removed the lines:

    if (showInput.ValueAsBoolean == false)

    {

    showInput.SetValue("true");

    return;

    }

    Now it takes the information, puts it in the hidden form, hides the top, unhides the bottom and exports the current view to a PDF. Now I have other things I want to do with it, but I will save that for other posts. =]

  • 05-22-2009 11:52 AM In reply to

    Re: Modify Boolean Value Programatically

    In the following statement you check whether showInput is null or not:

    if (null != showInput)

    If the field is in the datasource it will always have a value of True or False. So when you run the next statement, one of both will always run causing the code to execute the and then return (Exit the method). That's why the export part will never be executed.

    if (showInput.ValueAsBoolean == true)
    {
      showInput.SetValue(
    "false");
     
    return;
    }
    if (showInput.ValueAsBoolean == false)
    {
      showInput.SetValue(
    "true");
     
    return;
    }

    I would change the code to something like this:

    if (showInput.ValueAsBoolean == true)
    {
      showInput.SetValue(
    "false");
    }
    else 
    {
      showInput.SetValue(
    "true");
    }

     

  • 05-22-2009 12:38 PM In reply to

    Re: Modify Boolean Value Programatically

    Samir Farid:

    In the following statement you check whether showInput is null or not:

    if (null != showInput)

    If the field is in the datasource it will always have a value of True or False. So when you run the next statement, one of both will always run causing the code to execute the and then return (Exit the method). That's why the export part will never be executed.

    if (showInput.ValueAsBoolean == true)
    {
      showInput.SetValue(
    "false");
     
    return;
    }
    if (showInput.ValueAsBoolean == false)
    {
      showInput.SetValue(
    "true");
     
    return;
    }

    I would change the code to something like this:

    if (showInput.ValueAsBoolean == true)
    {
      showInput.SetValue(
    "false");
    }
    else 
    {
      showInput.SetValue(
    "true");
    }

     

     That makes sense. Thanks!

  • 05-22-2009 12:38 PM In reply to

    Re: Modify Boolean Value Programatically

    As Samir mentioned -- in my sample, I was using a return statement to exit the code, so, as you realized, you weren't ever getting to your export functionality... Glad you're all sorted out!

    Hilary Stoupa

Page 1 of 1 (12 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.