Set Value of Drop-Down List Box to Item #1 - InfoPath Dev
in

InfoPath Dev

Set Value of Drop-Down List Box to Item #1

Last post 08-27-2010 08:41 AM by ben.lenihan. 14 replies.
Page 1 of 1 (15 items)
Sort Posts: Previous Next
  • 04-29-2008 01:17 PM

    Set Value of Drop-Down List Box to Item #1

    I am using VSTA on a InfoPath form and I want to set a value on  a listbox to the first list item.  This list box is retrieving items from a list box on the SharePoint Site.

     What I am doing is I have a form that gets the userinfo from Active Directory and populates the form.  There are three items that initially I have to set manually, Directorate, Department, Division.  When I submit the form, all fields populate my list at my SharePoint site.  This works.  I am working on clearing the form using vb.net.  If a textbox is just a string textbox, it works, but errors when I try to set the Drop-Down List Box to "" or the first item in the list, which is blank.  Here is what I have so far.

     Dim xnMyForm As XPathNavigator = Me.CreateNavigator()
     Dim ns As XmlNamespaceManager = Me.NamespaceManager

     xnMyForm.SelectSingleNode("/my:SharePointTraining/my:fname", ns).SetValue("")   'WORKS
     xnMyForm.SelectSingleNode("/my:SharePointTraining/my:lname", ns).SetValue("")   'WORKS

     xnMyForm.SelectSingleNode("/my:SharePointTraining/my:directorate", ns).SetValue("")   'DOES NOT WORK


     TIA

    Mike
     


     

  • 04-29-2008 01:29 PM In reply to

    Re: Set Value of Drop-Down List Box to Item #1

    What is the data type of my:directorate?

    What is the error message you are getting?

    Is that node even present in the XML when you try to set it's value?

     

  • 04-29-2008 01:44 PM In reply to

    Re: Set Value of Drop-Down List Box to Item #1

     my:directorate is a drop-down list box and the data type is string which points to a SharePoint list that contains a list of all the directorates.

    The error is NullReference Exception 

    The message is "Message="Object reference not set to an instance of an object."
     

  • 04-30-2008 12:37 PM In reply to

    Re: Set Value of Drop-Down List Box to Item #1

    That kind of error message indicates that the select statement failed to retreive a valid node.

    You might try breaking out your statement to see if the selection failed (or walk through it in the debugger):

    XPathNaviagor test = xnMyForm.SelectSingleNode("/my:SharePointTraining/my:directorate", ns);
    if(test != null)
        test.SetValue("")
    else
        MessageBox.Show("Failed to select");

  • 05-06-2008 12:29 PM In reply to

    Re: Set Value of Drop-Down List Box to Item #1

     Still does not work.  Your code return the MessageBox.Show("Failed to select")

     

    This is just a drop-down box and I want to reset it to the first item in the list, which is blank.  I can submit the form with no problems but I just want to set the field to blank.  All the test boxes work because they are simple text boxes, but it seems that if the  text box is something else, InfoPath chokes.  There has to be an answer.

     

    Thanks,

     

    Mike 

  • 05-06-2008 01:02 PM In reply to

    Re: Set Value of Drop-Down List Box to Item #1

     I did not realize the XPathNavigator is case sensitive.  I did the following and it works, xnMyForm.SelectSingleNode("/my:SharePointTraining/my:Directorate", ns).SetValue("")

     

    Thanks,

     

    Mike 

  • 05-06-2008 04:25 PM In reply to

    Re: Set Value of Drop-Down List Box to Item #1

    Yes, all C# code is case sensitive. Hard lesson, but you'll not forget. Glad you got it working.

  • 05-08-2008 01:16 AM In reply to

    • fijaz
    • Not Ranked
    • Joined on 05-08-2008
    • Posts 3

    Re: Set Value of Drop-Down List Box to Item #1

    Hi,

    I want to assign a date value to DatePicker control (DateTime Data type) on my form. I have tried it using

    xnMyForm.SelectSingleNode("/my:Main/my:LeaveStartDate", ns).SetValue(DateTime.Today.ToString());

    But it gives following exception:

    System.InvalidOperationException
    Schema validation found non-data type errors.
       at Microsoft.MsoOffice.InfoPath.MsxmlInterop.MsxmlDocument.ThrowExceptionFromMsxmlInteropError(MsxmlInteropError eError)
       at Microsoft.MsoOffice.InfoPath.MsxmlInterop.MsxmlDocument.ThrowExceptionFromHresult(Int32 hrError)
       at Microsoft.MsoOffice.InfoPath.MsxmlInterop.MsxmlNodeImpl.set_Text(String strText)
       at Microsoft.Office.InfoPath.MsxmlNavigator.SetValue(String value)
      .......................

    I also tried

    xnMyForm.SelectSingleNode("/my:Main/my:LeaveStartDate", ns).SetTypedValue(DateTime.Today);

    and it returned following exception:

    System.NotImplementedException
    The method or operation is not implemented.
       at Microsoft.Office.InfoPath.MsxmlNavigator.SetTypedValue(Object typedValue)
      

    Any help?

  • 05-08-2008 04:26 PM In reply to

    Re: Set Value of Drop-Down List Box to Item #1

    .SetValue() is the right choice... but DateTime.Today.ToString() fails to give an XML-compatible date string.... XML expects the format of the string to be "YYYY-mm-DD", which you can get from a slight modification of your code: DateTime.Today.ToString("s"). The "s" tells the string generator to use the "s"ortable string format, which is, in fact, the XML format.

  • 05-08-2008 11:28 PM In reply to

    • fijaz
    • Not Ranked
    • Joined on 05-08-2008
    • Posts 3

    Re: Set Value of Drop-Down List Box to Item #1

    Greg Collins:

    .SetValue() is the right choice... but DateTime.Today.ToString() fails to give an XML-compatible date string.... XML expects the format of the string to be "YYYY-mm-DD", which you can get from a slight modification of your code: DateTime.Today.ToString("s"). The "s" tells the string generator to use the "s"ortable string format, which is, in fact, the XML format.

    Thanks, but still getting the same error shown below, also tried hardcoded date as .SetValue("2008-05-08").  

    System.InvalidOperationException
    Schema validation found non-data type errors.
       at Microsoft.MsoOffice.InfoPath.MsxmlInterop.MsxmlDocument.ThrowExceptionFromMsxmlInteropError(MsxmlInteropError eError)
       at Microsoft.MsoOffice.InfoPath.MsxmlInterop.MsxmlDocument.ThrowExceptionFromHresult(Int32 hrError)
       at Microsoft.MsoOffice.InfoPath.MsxmlInterop.MsxmlNodeImpl.set_Text(String strText)
       at Microsoft.Office.InfoPath.MsxmlNavigator.SetValue(String value)

  • 05-09-2008 04:17 PM In reply to

    Re: Set Value of Drop-Down List Box to Item #1

    The problem you are having might be coming from the xsi:nil attribute.

    On non-String data type nodes (including, but not limited to: Date, Time and DateTime nodes), if you want to allow the node to have a blank value, the schema places the attribute: xsi:nil="true" on the node. While this attribute is present on the node, the node value MUST, I repeat, MUST be blank. To set a value on such a node, you must first remove that attribute.

    Later, if you clear the value, and you don't want the field to be required (i.e. a red asterisk in the view on the field), then you must place that attribute back on the node.

    Hopefully this solves your issue.

  • 05-09-2008 04:45 PM In reply to

    Re: Set Value of Drop-Down List Box to Item #1

     Found my problem.

    Old code:

    xnMyForm.SelectSingleNode("/my:SharePointTraining/my:directorate", ns).SetValue("")  Don't work

     New Code:

    xnMyForm.SelectSingleNode("/my:SharePointTraining/my:Directorate", ns).SetValue("")  Works.

    Notice the capital "D" in directorate.  Did not realize that this is case sensitive. Someone told me that xPathNavigator is case sensitive.  

     

    Mike 

  • 05-12-2008 01:48 AM In reply to

    • fijaz
    • Not Ranked
    • Joined on 05-08-2008
    • Posts 3

    Re: Set Value of Drop-Down List Box to Item #1

    Greg Collins:

    The problem you are having might be coming from the xsi:nil attribute.

    On non-String data type nodes (including, but not limited to: Date, Time and DateTime nodes), if you want to allow the node to have a blank value, the schema places the attribute: xsi:nil="true" on the node. While this attribute is present on the node, the node value MUST, I repeat, MUST be blank. To set a value on such a node, you must first remove that attribute.

    Later, if you clear the value, and you don't want the field to be required (i.e. a red asterisk in the view on the field), then you must place that attribute back on the node.

    Hopefully this solves your issue.

    Yes, this was the problem. Thanks for the help.

  • 06-04-2008 10:55 AM In reply to

    Re: Set Value of Drop-Down List Box to Item #1

    Sure thing. Glad to help!

  • 08-27-2010 08:41 AM In reply to

    Re: Set Value of Drop-Down List Box to Item #1

    Helped me too

     Thanks a million

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