Simple if statement not working?? - InfoPath Dev
in

InfoPath Dev

Simple if statement not working??

Last post 12-16-2010 12:16 AM by paul_mc_87. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 12-15-2010 08:46 AM

    Simple if statement not working??

    Hi ,

     

    I have written many statements like the one below before, however this one simply wont work, I always get the messagebox appearing even if the conditions are not met, btw i realise i can achieve this with the GUI but i prefer to use code.

     It is attached to a sign button, whereby it simply checks if the person mentioned if the field matches the loggedonuser (which populated on form load), if so let them sign the field, if not then dont.

     However two more people (DistributedTo & distributedfurther) may possibly need to sign the form also so this needs to be taken into account (hence the piped (||) conditonal values) notice i have tried both || and | and neither seem to work. There will only ever be 3 people who can sign this form, approver, distributed to and distributedfurther......yes i know they are terrible string names but thats neither here not there :D

     

    thanks in advance, i will be suprosed if the code below formats correctly but i could not see a code insert tool.

    public void Sign_Section1_Clicked(object sender, ClickedEventArgs e)

    {

    {

     

    XPathNavigator Navigate = this.CreateNavigator();

    string approver = Navigate.SelectSingleNode("/my:SpecReviewForm/my:Nominated/my:Person/my:AccountId", NamespaceManager).Value.ToString();

    string loggedinuser = Navigate.SelectSingleNode("/my:SpecReviewForm/my:loggedinuser", NamespaceManager).Value.ToString();

    string DistributedTo = Navigate.SelectSingleNode("/my:SpecReviewForm/my:DistributedTo/my:Person/my:AccountId", NamespaceManager).Value.ToString();

    string signed = Navigate.SelectSingleNode("/my:SpecReviewForm/my:Section1/my:Signed", NamespaceManager).Value.ToString();string DistributedFurther = Navigate.SelectSingleNode("/my:SpecReviewForm/my:DistributeFurther/my:Person/my:AccountId", NamespaceManager).Value.ToString();

     

     

    if (DistributedFurther == "" | DistributedTo == "" | approver != loggedinuser)

    {

     

    MessageBox.Show("You are not authorised to sign this form");return;

    }

    else

     

    if ((approver != loggedinuser) | (DistributedTo != loggedinuser))

    {

    MessageBox.Show("You are not authorised to sign this form" + approver + loggedinuser + DistributedTo + DistributedFurther);return;

    }

     

     

    if (signed != "")

    {

    MessageBox.Show("You Cannot re-Sign this section");return;

     

    }

     

  • 12-15-2010 05:38 PM In reply to

    Re: Simple if statement not working??

    Using code in InfoPath where you don't have to is generally a bad idea.  I can tell from looking at your code that you seem to be rather inexperienced with coding, so that makes it a worse idea.

    You should be using || operators, not |.  And it looks like your logic is broken:

    if ((approver != loggedinuser) | (DistributedTo != loggedinuser))

    If the code gets to this if statement, it means that they will get that message unless the logged in user is both the approver and the distributedTo person.

    Have you tried walking through the code in the Debugger?  That should help you check the logic at realtime.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 12-15-2010 11:57 PM In reply to

    Re: Simple if statement not working??

    yes, have been through debugger.

     I thought that the statement would check either/or basically , does the | not do this?

     for both the conditions to be meant would that not mean i should be using an &

     all i want to achieve is that an approver can be the approver, distributedto or distributedfurther, messed about with all kinds of idea , try catch blocks as well for loops

  • 12-16-2010 12:16 AM In reply to

    Re: Simple if statement not working??

    another problem is that when i try and use rules to do this logic my signing even fires in the code behind also. It runs the rules and does everything ok but then the signing even still fires, signing event is below for your reference:

     

    try

    {

    // Get the user name of the current user.

    string userName = this.Application.User.UserName;

     

    // Create a DirectorySearcher object using the user name

    // as the LDAP search filter. If using a directory other

    // use sam account name to search and extract more fields, in this case first name and last name

    DirectorySearcher searcher = new DirectorySearcher("(sAMAccountName=" + userName + ")");

    // Search for the specified user.

    SearchResult result = searcher.FindOne();

    // Make sure the user was found.

    if (result == null)

    {

    MessageBox.Show("Error finding user: " + userName);

    }

    else

    {

    // Create a DirectoryEntry object to retrieve the collection

    // of attributes (properties) for the user.

    DirectoryEntry employee = result.GetDirectoryEntry();

    // Assign the specified properties to string variables.

    string FirstName = employee.Properties["givenName"].Value.ToString();

    string LastName = employee.Properties["sn"].Value.ToString();

    DateTime sign = DateTime.Now;string signature = "Signed by " + FirstName + " " + LastName + " On " + sign;

     

    // Create an XPathNavigator to walk the main data source

    // of the form.

    XPathNavigator xnMyForm = this.CreateNavigator();

    XmlNamespaceManager ns = this.NamespaceManager;

    // Set the fields in the form.

    xnMyForm.SelectSingleNode("/my:SpecReviewForm/my:Section2/my:Sign2", ns).SetValue(signature);

     

     

     

    // Clean up.

    xnMyForm = null;

    searcher.Dispose();

    result =
    null;

    employee.Close();

    }

    }

    catch (Exception ex)

    {

    MessageBox.Show("The following error occurred: " +

    ex.Message.ToString());

    throw;

    }

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