Set Value of Drop-Down List Box to Item #1 - InfoPath Dev Sign in | Join | Help in Newbie Questions InfoPath (Entire Site) InfoPath Dev InfoPath Dev is dedicated to bringing you the information and tools you need to be successful in your Microsoft Office InfoPath development projects. Home Blogs Forums Photos Downloads InfoPath Dev » InfoPath » Newbie Questions » Set Value of Drop-Down List Box to Item #1 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: Oldest to newest Newest to oldest Previous Next 04-29-2008 01:17 PM Michael Vasquez Joined on 04-08-2008 Chula Vista Posts 54 Set Value of Drop-Down List Box to Item #1 Reply Contact 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 TIAMike 04-29-2008 01:29 PM In reply to Greg Collins Joined on 08-07-2006 Posts 2,126 Re: Set Value of Drop-Down List Box to Item #1 Mark as Not AnswerMark as Answer... Reply Contact 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? Please Visit:BraintroveGreg's Paintings 04-29-2008 01:44 PM In reply to Michael Vasquez Joined on 04-08-2008 Chula Vista Posts 54 Re: Set Value of Drop-Down List Box to Item #1 Mark as Not AnswerMark as Answer... Reply Contact 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 Greg Collins Joined on 08-07-2006 Posts 2,126 Re: Set Value of Drop-Down List Box to Item #1 Mark as Not AnswerMark as Answer... Reply Contact 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"); Please Visit:BraintroveGreg's Paintings 05-06-2008 12:29 PM In reply to Michael Vasquez Joined on 04-08-2008 Chula Vista Posts 54 Re: Set Value of Drop-Down List Box to Item #1 Mark as Not AnswerMark as Answer... Reply Contact 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 Michael Vasquez Joined on 04-08-2008 Chula Vista Posts 54 Re: Set Value of Drop-Down List Box to Item #1 Mark as Not AnswerMark as Answer... Reply Contact 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 Greg Collins Joined on 08-07-2006 Posts 2,126 Re: Set Value of Drop-Down List Box to Item #1 Mark as Not AnswerMark as Answer... Reply Contact Yes, all C# code is case sensitive. Hard lesson, but you'll not forget. Glad you got it working. Please Visit:BraintroveGreg's Paintings 05-08-2008 01:16 AM In reply to fijaz Joined on 05-08-2008 Posts 3 Re: Set Value of Drop-Down List Box to Item #1 Mark as Not AnswerMark as Answer... Reply Contact 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.InvalidOperationExceptionSchema 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.NotImplementedExceptionThe 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 Greg Collins Joined on 08-07-2006 Posts 2,126 Re: Set Value of Drop-Down List Box to Item #1 Mark as Not AnswerMark as Answer... Reply Contact .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. Please Visit:BraintroveGreg's Paintings 05-08-2008 11:28 PM In reply to fijaz Joined on 05-08-2008 Posts 3 Re: Set Value of Drop-Down List Box to Item #1 Mark as Not AnswerMark as Answer... Reply Contact 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.InvalidOperationExceptionSchema 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 Greg Collins Joined on 08-07-2006 Posts 2,126 Re: Set Value of Drop-Down List Box to Item #1 Mark as Not AnswerMark as Answer... Reply Contact 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. Please Visit:BraintroveGreg's Paintings 05-09-2008 04:45 PM In reply to Michael Vasquez Joined on 04-08-2008 Chula Vista Posts 54 Re: Set Value of Drop-Down List Box to Item #1 Mark as Not AnswerMark as Answer... Reply Contact 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 Joined on 05-08-2008 Posts 3 Re: Set Value of Drop-Down List Box to Item #1 Mark as Not AnswerMark as Answer... Reply Contact 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 Greg Collins Joined on 08-07-2006 Posts 2,126 Re: Set Value of Drop-Down List Box to Item #1 Mark as Not AnswerMark as Answer... Reply Contact Sure thing. Glad to help! Please Visit:BraintroveGreg's Paintings 08-27-2010 08:41 AM In reply to ben.lenihan Joined on 10-09-2007 Posts 3 Re: Set Value of Drop-Down List Box to Item #1 Mark as Not AnswerMark as Answer... Reply Contact 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.
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
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?
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."
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");
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,
I did not realize the XPathNavigator is case sensitive. I did the following and it works, xnMyForm.SelectSingleNode("/my:SharePointTraining/my:Directorate", ns).SetValue("")
Yes, all C# code is case sensitive. Hard lesson, but you'll not forget. Glad you got it working.
Hi,
I want to assign a date value to DatePicker control (DateTime Data type) on my form. I have tried it using
xnMyForm.SelectSingleNode(
System.InvalidOperationExceptionSchema 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) .......................
and it returned following exception:
System.NotImplementedExceptionThe method or operation is not implemented. at Microsoft.Office.InfoPath.MsxmlNavigator.SetTypedValue(Object typedValue)
Any help?
.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.
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.InvalidOperationExceptionSchema 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)
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.
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.
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.
Sure thing. Glad to help!
Helped me too
Thanks a million