Hi
I am trying to use the selected item within a combobox to store it in a variable.
this variable is then going to be used in another command for sorting a SQL Query.
I have tested to make sure that the issue is not the SQL Query, as manually entering the value, as opposed to using a variable works just fine and the query itself already uses other variables with no issue.
This is the SQL query in question:
"Select * FROM Database.dbo.table order by $variable;"
It appears that for whatever reason the combobox is returning a null output.
I have tested this by using write-host $variable (the variable used to store the selecteditem in the combobox)
I have set the combobox to use a collection of items and the style is set to dropdownlist.
I have tried passing using the .text, .selecteditem, .selectedvalue, and .selectedIndex.
The first three return nothing and the last returns -1.
Also as a final note, how do I make it so the combobox auto selects the first item.
Thank you for your help.
Combobox returns Null
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
- i-worx.admin
- Posts: 6
- Last visit: Wed Feb 07, 2024 12:04 pm
Re: Combobox returns Null
jvierra wrote:Just reference the combobox directly.
"Select * FROM Database.dbo.table order by $($combobox1.SelectedItem)"
That worked!!. I managed to get it to work another way as well, by creating functions. I think the issue I am having is mainly do to the fact that when my form loads, the combobox is blank.
Is there a way to have it load with the top value selected?
- i-worx.admin
- Posts: 6
- Last visit: Wed Feb 07, 2024 12:04 pm
Re: Combobox returns Null
Sadly that did not work.
Here is what I receive when I input that in to the script
ERROR: Exception setting "SelectedIndex": "InvalidArgument=Value of '1' is not valid for 'SelectedIndex'.
ERROR: Parameter name: SelectedIndex"
MainForm.psf (28, 1): ERROR: At Line: 28 char: 1
ERROR: + $combobox1.SelectedIndex = 1
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
ERROR: + FullyQualifiedErrorId : ExceptionWhenSetting
ERROR:
WARNING: Incorrect syntax near ';'.
WARNING: Collection cannot be null.
Parameter name: c
Here is what I receive when I input that in to the script
ERROR: Exception setting "SelectedIndex": "InvalidArgument=Value of '1' is not valid for 'SelectedIndex'.
ERROR: Parameter name: SelectedIndex"
MainForm.psf (28, 1): ERROR: At Line: 28 char: 1
ERROR: + $combobox1.SelectedIndex = 1
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
ERROR: + FullyQualifiedErrorId : ExceptionWhenSetting
ERROR:
WARNING: Incorrect syntax near ';'.
WARNING: Collection cannot be null.
Parameter name: c