Monday, October 8, 2012

Set the value of a field II

Note that a few controls in InfoPath such as for example date picker controls have a nil attribute set o the fields they are bound to when they are empty. This nil attribute must be removed before you can set the value of such fields, otherwise you will get the following error when you try to set the field's value through code:

Schema validating found non-data type errors

In order to remove the nil attribute from a date picker control add the following code:

Dim mainDS As XPathNavigator = MainDataSource.CreateNavigator
Dim StartDate As XPathNavigator = mainDS.SelectSingleNode("/my:myFields/my:StartDate", NamespaceManager)

If StartDate.MoveToAttribute("nil", NamespaceManager.LookupNamespace("xsi")) Then
    StartDate.DeleteSelf()
End If

StartDate.SetValue("2012-10-08")

The following InfoPath controls typically have nil attributes on the fields they are bound to:
  • Text Box with the Whole Number (integer) data type
  • Text Box with the Decimal (double) data type
  • Text Box with the Time (time) data type
  • Date Picker
  • Date and Time Picker
  • File Attachment
  • Picture (when a picture is included in the form instead of as a link)
  • Ink Pickture
You can find out whether a control supports the nil attribute by adding a control to the form template, previewing the form, leaving the control empty, saving the form locally on disk, opening the XML file for the form in Notepad, and then looking for:

xsi:nil="true"


No comments:

Post a Comment

About Me

My photo
Toronto, Ontario, Canada
MBA, M.Sc. & MCP