locked
InfoPath Browser Form - File Attachment Validation Error RRS feed

  • Question

  • I have the following code in my InfoPath form:

     public void InternalStartup()
            {
                EventManager.XmlEvents["/my:Form_PrePlan/my:Attachments/my:cddFile"].Validating += new XmlValidatingEventHandler(cddFile_Validating);
            }
            public void cddFile_Validating(object sender, XmlValidatingEventArgs e)
            {
                XPathNavigator errNode = this.CreateNavigator().SelectSingleNode(e.Match.Expression, NamespaceManager);
                string fileName = returnFileName(new MemoryStream(Convert.FromBase64String(errNode.Value)));
    
                if (fileName.Length > 4 && !(fileName.Substring(fileName.Length -4,4) == ".cdd"))
                {
                    this.Errors.Add(errNode, "cddValidation","File needs to be a file.", "The attached file must be a cdd file.");        
                }
            }
    
    public string returnFileName(MemoryStream ms)
            {
                Encoding enc = Encoding.Unicode;
                string fileName = "";
    
                if (ms.Length > 0)
                {
                using (ms)
                {
                    BinaryReader reader = new BinaryReader(ms);
                    byte[] headerData = reader.ReadBytes(16);
                    int fileSize = (int)reader.ReadUInt32();
                    int attachmentNameLength = (int)reader.ReadUInt32() * 2;
                    byte[] fileNameBytes = reader.ReadBytes(attachmentNameLength);
                    fileName = enc.GetString(fileNameBytes, 0, attachmentNameLength - 2);
                }
            }
                return fileName;
            }

     

    When I preview this form in InfoPath Designer and trigger the validation everything works correctly.  However, once I publish the form to the Forms Library in SharePoint I receive the following error in the ULS logs:

    "Business logic failed due to an exception.  Calling this property or method from a hosting page is not supported.  My guess is one of the references I added isn't supported (added references: System.IO, System.Text)." Ideas?



    • Edited by xtreme9 Tuesday, February 7, 2012 5:59 PM
    Tuesday, February 7, 2012 5:59 PM

Answers

  • There are some properties or methods that cannot be hosted in .Net page. One that I know is “Errors”. Try to use something else, or I read a few articles about using Changed Event instead of Validating Event long time ago. You may try this approach if necessary.




    백상하 - http://blog.naver.com/agilepoint


    • Edited by Sangha Baek MVP Tuesday, February 7, 2012 6:57 PM
    • Marked as answer by xtreme9 Wednesday, February 8, 2012 4:43 PM
    Tuesday, February 7, 2012 6:55 PM
  • I changed the validation event over to a changed event for the File Attachment and it seems to be working now.  However, I'm not sure which property or method was causing the problem.  I don't think it was the "Errors" because the validation event for the Coordinates works correctly.  Weird.

    Thank you for your assistance though; it did help me resolve the problem.

    • Marked as answer by xtreme9 Wednesday, February 8, 2012 4:43 PM
    Wednesday, February 8, 2012 4:43 PM

All replies

  • There are some properties or methods that cannot be hosted in .Net page. One that I know is “Errors”. Try to use something else, or I read a few articles about using Changed Event instead of Validating Event long time ago. You may try this approach if necessary.




    백상하 - http://blog.naver.com/agilepoint


    • Edited by Sangha Baek MVP Tuesday, February 7, 2012 6:57 PM
    • Marked as answer by xtreme9 Wednesday, February 8, 2012 4:43 PM
    Tuesday, February 7, 2012 6:55 PM
  • Curious, you say "Errors" cannot be hosted in a .NET page.  I have the following function in the same InfoPath form and it works without any problems:

     public void Coord_Validating(object sender, XmlValidatingEventArgs e)
            {
                XPathNavigator errNode = this.CreateNavigator().SelectSingleNode(e.Match.Expression, NamespaceManager);
    
                string coordString, coordStringMessage, coordStringDetailedMessage;
                Boolean isX = false;
                // Determines which coordinate is being validated.
                if (e.Match.Expression.Contains("xCoord"))
                {
                    coordString = "x-coordinate";
                    coordStringMessage = "Format is -##.######";
                    coordStringDetailedMessage = "Format needs to be -##.### or -##.###### format.  Be sure to include the negative symbol and decimal.";
                    isX = true;
                }
                else
    	        {
                    coordString = "y-coordinate";
                    coordStringMessage = "Format is ##.######";
                    coordStringDetailedMessage = "Format needs to be ##.### or ##.###### format.  Be sure to include a decimal.";
                    isX = false;
    	        }
    
                // Calls the function to validate the coordinate and add the error if it's invalid.
                if (! validateCoord(errNode.Value, isX) && errNode.Value.Length != 0)
                {
                    this.Errors.Add(errNode, coordString, coordStringMessage, coordStringDetailedMessage);        
                }
            }

    Tuesday, February 7, 2012 7:53 PM
  • It is for XmlFormView controls for showing InfoPath form in ASP.NET page. I think your case is different, sorry for confusion.



    백상하 - Stop hacking my blog http://blog.naver.com/agilepoint


    Tuesday, February 7, 2012 8:50 PM
  • I changed the validation event over to a changed event for the File Attachment and it seems to be working now.  However, I'm not sure which property or method was causing the problem.  I don't think it was the "Errors" because the validation event for the Coordinates works correctly.  Weird.

    Thank you for your assistance though; it did help me resolve the problem.

    • Marked as answer by xtreme9 Wednesday, February 8, 2012 4:43 PM
    Wednesday, February 8, 2012 4:43 PM