Setting Date value to Datetime picker at runtime In Infopath Form - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

Setting Date value to Datetime picker at runtime In Infopath Form

Last post 09-21-2023 02:49 PM by Hilary Stoupa. 12 replies.
Page 1 of 1 (13 items)
Sort Posts: Previous Next
  • 10-20-2008 02:17 PM

    Setting Date value to Datetime picker at runtime In Infopath Form

    I have a date time picker to which i need to set custom date at runtime. I know that if i make datatype as Text of datetime picker there is no issue setting the date at runtime but due to comparision with other datetime picker strictly i have to keep datatype as Date.

    When i try to set date of DateTime picker  in Infopath form using C# code it gives me XSD validation error which is very true. Googled but no answer. Do let me know if question satisfied or need more info.

    Don't ask me to share my XSN file.

  • 10-20-2008 08:02 PM In reply to

    • Shiva
    • Top 25 Contributor
    • Joined on 04-15-2005
    • India
    • Posts 694

    Re: Setting Date value to Datetime picker at runtime In Infopath Form

    Normal 0 false false false MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;}

    Hello,

    Whenever you trying to set the date value to node it should be format of string only. Before setting the value, you have to convert it into string then try to set this value.

    Date = DateTime.Now.ToString("yyyy-MM-dd");

    Xdocument.SelectSingleNode("<DateNodeXPath.").text = Date;

     
  • 10-21-2008 12:20 AM In reply to

    Re: Setting Date value to Datetime picker at runtime In Infopath Form

    I am alread doing that.  

    XPathNavigator newNode = myNav.SelectSingleNode("//my:field7", this.NamespaceManager); 

    newNode.SetValue(DateTime.ParseExact("20/10/2008","dd/MM/yyyy",null).ToShortDateString());

     My form is browser form having DatePicker control with datatype as date. If u check Infopath schema it has datatype as date and i am trying to set the string value which is very obvious to generate schema validation.

     

    PRashant

  • 10-21-2008 07:48 AM In reply to

    Re: Setting Date value to Datetime picker at runtime In Infopath Form

    I think you may be running into trouble with the xsi:nil attribute -- When you set the value of your date time field, you'll need to remove that attribute. It throws a schema validation error otherwise. See this article for more info: http://blogs.msdn.com/infopath/archive/2006/11/28/the-xsi-nil-attribute.aspx

    Hilary Stoupa

  • 10-22-2008 01:20 AM In reply to

    Re: Setting Date value to Datetime picker at runtime In Infopath Form

    Now i managed to avoid the schema validation error but still i am unable to set the date value programmatically. After setting date programmatically get error on infopath form "Only Date allowed". This is really pain from microsoft.While setting the date programmatically using setValue i can set only string value but DatePicker in infopath has data type as date.

  • 10-22-2008 07:49 AM In reply to

    Re: Setting Date value to Datetime picker at runtime In Infopath Form

    Managed to solve this issue. In reality Infopath doesn't like any other date format except yyyy-MM-dd.

    Eg. as below


                    XPathNavigator myNav = this.MainDataSource.CreateNavigator();               
                    XPathNavigator workflowDataNav = this.DataSources[0].CreateNavigator();               
                    XPathNavigator newNode = myNav.SelectSingleNode("//my:field7", this.NamespaceManager);

                    if (newNode.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance"))
                        newNode.DeleteSelf();
                    newNode.SetValue(Convert.ToDateTime(DateTime.Now).ToString("yyyy-MM-dd"));

    You can format separate date format in the date picker in Infopath form choice is urs....

     

  • 10-25-2008 03:15 AM In reply to

    Re: Setting Date value to Datetime picker at runtime In Infopath Form

    You can also use the XmlConvert.ToString() method to convert a DateTime to an XML date, though it doesn't make much of a difference in this case. You still have to provide a format string since InfoPath won't accept dates with the time in them:

    newNode.SetValue(XmlConvert.ToString(DateTime.Now, "yyyy-MM-dd"));

    In either case, there's no need to convert DateTime.Now to a DateTime object, as it's already a DateTime object. 

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 03-31-2009 04:35 AM In reply to

    Re: Setting Date value to Datetime picker at runtime In Infopath Form

    While the following is a bit long winded it does work, I would expect a DateTime format provider to give a much more succinct block of code, but this gives precision.

    Based upon an InfoPath DateTime Data Type field with its display format set to show either Date or Time, the following C# will populate this field:

    string strNow = DateTime.Now.Year.ToString() + "-" +
                            (DateTime.Now.Month.ToString().Length == 1 ? "0" + DateTime.Now.Month.ToString() : DateTime.Now.Month.ToString()) + "-" +
                            (DateTime.Now.Day.ToString().Length == 1 ? "0" + DateTime.Now.Day.ToString() : DateTime.Now.Day.ToString()) + "T" +
                            (DateTime.Now.Hour.ToString().Length == 1 ? "0" + DateTime.Now.Hour.ToString() : DateTime.Now.Hour.ToString()) + ":" +
                            (DateTime.Now.Minute.ToString().Length == 1 ? "0" + DateTime.Now.Minute.ToString() : DateTime.Now.Minute.ToString()) + ":" +
                            (DateTime.Now.Second.ToString().Length == 1 ? "0" + DateTime.Now.Second.ToString() : DateTime.Now.Second.ToString());
    objStartDateTimeNode.SetValue(strNow);
                        // e.g. This is how the date format needs to be for a DateTime with time only display field - objStartDateTimeNode.SetValue("2009-01-29T01:12:21");

     Note the last comment showing the needed format for the inbound date time string.

    Andrew Maisey
  • 03-31-2009 05:23 AM In reply to

    Re: Setting Date value to Datetime picker at runtime In Infopath Form

    Thank you for your contribution, but the following pair of lines will produce exactly the same result:

    string strNow = XmlConvert.ToString(DateTime.Now, "yyyy-MM-ddTHH:mm:ss");
    objStartDateTimeNode.SetValue(strNow);

    The following will also work just fine:

    objStartDateTimeNode.SetValue(XmlConvert.ToString(System.DateTime.Now));

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 07-14-2009 09:18 AM In reply to

    Re: Setting Date value to Datetime picker at runtime In Infopath Form

    Sorry to bump an old thread, but is there any way to do this without any code-behind?

    What I'm trying to do is promote this field as a Date to a SharePoint Library. And what I'm hoping to do is filter out forms where this field is greater than or equal to [Today]-2. (In other words, I only want to see forms that are 2 days past this end date.

    here is what I've got. This is the ONLY way, I can get the form to properly accept my datetime field without throwing validation errors.

    My Form has a dataconnection to a web-service that is making a SQL call to a SQL 2K5 database.

     Web-service call: "Select top 1 pe_end_date from pay_periods where active = 1"

    And my field is equal to that value and is of datetime data type. And it properly accepts and formats the field. However when this gets promoted to my SharePoint list; it gets promoted as a single line of text and thus displays "2009-07-11T00:00:00-05:00" as the value.

    Consequently, I cannot do any calculations on this field in order to filter by it. Can anyone give me any suggestions? Should I move this to SharePoint forum?

     

  • 07-14-2009 09:41 AM In reply to

    Re: Setting Date value to Datetime picker at runtime In Infopath Form

    Actually..it dawned on me after I posted to make a calculated column that parse's the text and converts to a date..that worked. thanks anyway.

  • 09-21-2023 12:20 PM In reply to

    Re: Setting Date value to Datetime picker at runtime In Infopath Form

    This is awesome. It's 2023 and I'm trying to populate a DatePicker control in InfoPath 2013 from SQL Server for use in SharePoint 2016, and this works! Thank you!
    prashant.raut:

    Managed to solve this issue. In reality Infopath doesn't like any other date format except yyyy-MM-dd.

    Eg. as below


                    XPathNavigator myNav = this.MainDataSource.CreateNavigator();               
                    XPathNavigator workflowDataNav = this.DataSources[0].CreateNavigator();               
                    XPathNavigator newNode = myNav.SelectSingleNode("//my:field7", this.NamespaceManager);

                    if (newNode.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance"))
                        newNode.DeleteSelf();
                    newNode.SetValue(Convert.ToDateTime(DateTime.Now).ToString("yyyy-MM-dd"));

    You can format separate date format in the date picker in Infopath form choice is urs....

     

  • 09-21-2023 02:49 PM In reply to

    Re: Setting Date value to Datetime picker at runtime In Infopath Form

    Glad the forum could help - I still look stuff up here when I get stuck! :)
    Hilary Stoupa

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