Updating the Field/Attribute is rather easy, the problem is when that Field/Attribute is displayed in a date picker control. The reason for it is the format of the DateTime string. In order, InfoPath to parse out the datetime correctly from the Field/Attribute and display it correctly (pass validation) you have to use the following formats:
1. For displaying dates only:”yyyy-MM-dd”
2. For displaying date and time: “yyyy’-'MM’-'dd’T'HH’:'mm’:'ss”
So right before you set the value in the XPathNavigator field/attribute, convert the date you want to use to a string using one of the two formats above.
Example:

DateTime today=DateTime.Now;
XPathNavigator myfield=MainDataSource.CreateNavigator()
.SelectSingleNode("xpath to field",this.NamespaceManager);
String dateString=today.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss");
myfield.SetValue(dateString);
 

2 Responses to InfoPath: Update DateTime Field/Attribute using C#

  1. Yvette says:

    FYI:
    today.ToStrong should be today.ToString

  2. ivan.markov says:

    Thanks, fixed it.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Set your Twitter account name in your settings to use the TwitterBar Section.