Date in a date picker from a date object - InfoPath Dev
in

InfoPath Dev

Date in a date picker from a date object

Last post 08-25-2009 07:14 PM by tfiema. 5 replies.
Page 1 of 1 (6 items)
Sort Posts: Previous Next
  • 04-17-2008 05:42 AM

    Date in a date picker from a date object

    Can I put a date in a date picker from a date object using code?

     

    I tried using many different ways but always but always the date returned is in a format different of the expected by the date picker.

    I only have sucess using string, but I want to try using a date object.


    Anyone know how to format the date object to tranfer the values to a date picker?

     


    After that I ll use two date values to make a calendar exibition.

     

     

    Thanks!

  • 04-17-2008 10:36 AM In reply to

    Re: Date in a date picker from a date object

    XML, and thus InfoPath, expect a date to be formatted like: 2008-04-17 (yyyy-MM-dd), and a datetime to be formatted as 2008-04-17T11:34:23 (yyyy-MM-ddThh:mm:ss).

    You must also, when populating a date node via code, ensure that the xsi:nil attribute is removed before trying to set the value, or you will receive an error. The use of xsi:nil="true" means that you won't see a red asterisk on the date picker field.... but it also means that it is expecting the field value to be blank. So you must remove the attribute BEFORE setting the value. When clearing the value, you will likely want ot add the attribute back onto the node.

  • 04-17-2008 10:37 AM In reply to

    Re: Date in a date picker from a date object

    I should mention that the string data type can be blank whenever it wants and does not use the xsi:nil attribute. Formatting is not important on a string type either.

  • 04-07-2009 01:15 PM In reply to

    Re: Date in a date picker from a date object

    Great info Greg!!, thanks

    just pointing to msdn info where "how to" copy code

    http://msdn.microsoft.com/en-us/library/bb509311.aspx

     public void DeleteNil(XPathNavigator node)
    {
       if (node.MoveToAttribute(
          "nil", "http://www.w3.org/2001/XMLSchema-instance"))
          node.DeleteSelf();
    }

     

    // Access the main data source.
    XPathNavigator myForm = MainDataSource.CreateNavigator();

    // Select the field.
    XPathNavigator myDate = myForm.SelectSingleNode("/my:myFields/my:myDate", NamespaceManager);

    // Check for and remove the "nil" attribute.
    DeleteNil(myDate);

    // Build the current date in the proper format. (yyyy-mm-dd)
    string curDate = DateTime.Today.Year + "-" + DateTime.Today.Month +
       "-" + DateTime.Today.Day;

    // Set the value of the myDate field.
    myDate.SetValue(strCurDate);

  • 04-07-2009 04:35 PM In reply to

    Re: Date in a date picker from a date object

    Thank you for your contribution, esorio! 

    eosorio:
    // Build the current date in the proper format. (yyyy-mm-dd)
    string curDate = DateTime.Today.Year + "-" + DateTime.Today.Month + "-" + DateTime.Today.Day;

    This will not actually put the date in the correct format if Month or Day is a single digit on the day when the code is run.  I recommend using the following:

    string curDate = DateTime.Today.ToString("yyyy-MM-dd");

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 08-25-2009 07:14 PM In reply to

    • tfiema
    • Not Ranked
    • Joined on 09-17-2008
    • Posts 6

    Re: Date in a date picker from a date object

    to set date picker value from code you need to set its nil attribute to false. To do that use the toggle code below. but toggling it back to ture is a bit tricky. I usually set the value of the datepicker to "" from a rule and then use the toggle code to set the nil attrib to true (rules fire first code comes after). I wouldn't delete the attribute, you might need to recreate it later and that's just more work.

    private void ToggleXsiNilInMain(string xpath, string attValue){

    XPathNavigator mainNode = MainDataSource.CreateNavigator().SelectSingleNode(xpath + "/@xsi:nil", NamespaceManager);

    if (mainNode != null){mainNode.SetValue(attValue);}

    }

     

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