DevSupport
asked on
powershell code to list items in dropdown
Hi
I am using the below to generate a list of databases from a database server based on selection from list box:
Please see the populatedblist2 function, I am able to get the list of databases into the combo box.
However, when I click the button again by selecting a different item in the Listbox, I am not able to retrieve results again.
In other words I can load the names from select query only once. Please let me know how to regenerate this list in my subsequent button presses.
Thanks
I am using the below to generate a list of databases from a database server based on selection from list box:
$objLabel0 = New-Object System.Windows.Forms.Label
$objLabel0.Location = New-Object System.Drawing.Size(20,22)
$objLabel0.Size = New-Object System.Drawing.Size(280,20)
$objLabel0.Text = "Select the Source Database Group Name:"
$objForm.Controls.Add($objLabel0)
$objListBox0 = New-Object System.Windows.Forms.ListBox
$objListBox0.Location = New-Object System.Drawing.Size(20,42)
$objListBox0.Size = New-Object System.Drawing.Size(260,20)
$objListBox0.Height = 80
[void] $objListBox0.Items.Add("server1")
[void] $objListBox0.Items.Add("Server2")
.
.
.
$objForm.Controls.Add($objListBox0)
$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Size(20,120)
$objLabel1.Size = New-Object System.Drawing.Size(280,20)
$objLabel1.Text = "Enter Source Database Name:"
$objForm.Controls.Add($objLabel1)
$objButton2 = New-Object System.Windows.Forms.Button
$objButton2.Location = New-Object System.Drawing.Size(290,140)
$objButton2.Size = New-Object System.Drawing.Size(75,20)
$objButton2.Text = "Load DBs"
$objButton2.Add_Click({PopulateDBList2})
$objForm.Controls.Add($objButton2)
function PopulateDBList2{
$objDropDown1 = New-Object System.Windows.Forms.ComboBox
$objDropDown1.Location = New-Object System.Drawing.Size(20,140)
$objDropDown1.Size = New-Object System.Drawing.Size(260,15)
$DBNamearr1 = (Invoke-SQLCmd -query "SELECT name FROM sys.databases where database_id > '4'" -Server $objListBox0.SelectedItem)
$objDropDown1.Items.Clear();
ForEach ($item in $DBNamearr1){
$objDropDown1.items.add($item.Name)
}
$objForm.Controls.Add($objDropDown1)
}
Please see the populatedblist2 function, I am able to get the list of databases into the combo box.
However, when I click the button again by selecting a different item in the Listbox, I am not able to retrieve results again.
In other words I can load the names from select query only once. Please let me know how to regenerate this list in my subsequent button presses.
Thanks
ASKER
Please let me know if there is no solution and I will work on some alternative solution.
Thanks
Thanks
I cannot see any reason for that behaviour, so - did you check if the populatedblist2 code is been executed again when you click the button?
To help further, we would need to have runnable code, reduced to the minimum possible.
To help further, we would need to have runnable code, reduced to the minimum possible.
ASKER
I can see that the populatedblist2 function is executing again. I did a test write-host command and that works each time I press button but the drop down list does not change.
Thanks
Amrith
Thanks
Amrith
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am attaching the full code if you need.
Thanks
powershellform_refresh.txt