none
InfoPath 2010 Code not working RRS feed

  • Question

  • Hi,

     

    I have following code for a button click event. What i am trying to do is when I click this button i want to check for a particular field and if that field is blank, then display a message saying that the field cannot be blank. The section where the button is contained is p:Component and p:ProbablityTrigger is p:Component 's child element. So am i doing something wrong with the XPathnavigator object?

     

    public void btncalculate_Clicked(object sender, ClickedEventArgs e)

    {

     

    // Write your code here.

     

     

    string myNamespace = NamespaceManager.LookupNamespace("p");

     

    XPathNavigator xdoc = e.Source;

     

    try
     

    {

     

    XPathNavigator probabilityTrigger = xdoc.SelectSingleNode("p:ProbabilityTrigger");

     

    if (probabilityTrigger == null || probabilityTrigger.InnerXml == "")

    {

     

    MessageBox.Show("Probability of Trigger cannot be blank");

    }

     

    }

     

    catch
     

    {

     

    }

     

     

     

     

     

     

     

    }

    Please help!

     

     

    Saturday, February 4, 2012 6:37 AM

Answers

  • Hi Cooltechie1234,

    Thank you for posting.

    I think that there is something wrong with the SelectSingleNode method, I added a textbox and use the following code snippet:

            public void btncalculate_Clicked(object sender, ClickedEventArgs e)
            {
                // Write your code here. 
    
                string myNamespace = NamespaceManager.LookupNamespace("my");
    
                XPathNavigator xdoc = e.Source;
                try
                {
                    XPathNavigator probabilityTrigger = xdoc.SelectSingleNode("/my:myFields/my:field1", NamespaceManager);
    
                    if (probabilityTrigger == null || probabilityTrigger.InnerXml == "")
                    {
                        MessageBox.Show("Probability of Trigger cannot be blank");
                    }
    
                }
                catch
                {
                }
            }
    

    It can work well on my side. I think you may need to change the path to be full and include the second parameter NamespaceManager.

    Best Regards


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Bruce Song Thursday, February 16, 2012 8:29 AM
    Monday, February 6, 2012 3:40 AM
  • Hi Cooltechie1234, 
    How about the problem on your side? Do you still need any assistant about the problem? If you still show any concern on the problem, just feel free to let us know. 
    Best Regards,

    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Cooltechie1234 Tuesday, August 14, 2012 11:52 AM
    Wednesday, February 15, 2012 2:21 AM

All replies

  • Hi Cooltechie1234,

    Thank you for posting.

    I think that there is something wrong with the SelectSingleNode method, I added a textbox and use the following code snippet:

            public void btncalculate_Clicked(object sender, ClickedEventArgs e)
            {
                // Write your code here. 
    
                string myNamespace = NamespaceManager.LookupNamespace("my");
    
                XPathNavigator xdoc = e.Source;
                try
                {
                    XPathNavigator probabilityTrigger = xdoc.SelectSingleNode("/my:myFields/my:field1", NamespaceManager);
    
                    if (probabilityTrigger == null || probabilityTrigger.InnerXml == "")
                    {
                        MessageBox.Show("Probability of Trigger cannot be blank");
                    }
    
                }
                catch
                {
                }
            }
    

    It can work well on my side. I think you may need to change the path to be full and include the second parameter NamespaceManager.

    Best Regards


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Bruce Song Thursday, February 16, 2012 8:29 AM
    Monday, February 6, 2012 3:40 AM
  • Hi Cooltechie1234, 
    How about the problem on your side? Do you still need any assistant about the problem? If you still show any concern on the problem, just feel free to let us know. 
    Best Regards,

    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Cooltechie1234 Tuesday, August 14, 2012 11:52 AM
    Wednesday, February 15, 2012 2:21 AM