From 397e8ede04aef305d52f25b7d168118e3f0bc2d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Doma=C5=84ski?= Date: Thu, 11 Jun 2026 10:22:34 +0000 Subject: [PATCH] Jun 11, 2026, 12:22 PM --- toolbox/DBAToolBox.ps1 | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/toolbox/DBAToolBox.ps1 b/toolbox/DBAToolBox.ps1 index 9d336ec..44a7e24 100644 --- a/toolbox/DBAToolBox.ps1 +++ b/toolbox/DBAToolBox.ps1 @@ -49,6 +49,9 @@ $btnAddServer = $Window.FindName("btnAddServer") $btnRemoveServer = $Window.FindName("btnRemoveServer") $gridDetails = $Window.FindName("gridDetails") +# Initialize connection states cache per server +$script:ServerConnectionStates = @{} + # Set initial values from settings $txtServerName.Text = $settings.LastServer @@ -141,17 +144,38 @@ $tvServers.Add_SelectedItemChanged({ $gridDetails.Visibility = "Visible" if ($selectedItem.Tag -is [string]) { - # It's a server node, copy its name to TextBox - $txtServerName.Text = $selectedItem.Tag - Write-Log "Selected server: $($selectedItem.Tag)" + # It's a server node, copy its name to TextBox and update its cached connection state + $srvName = $selectedItem.Tag + $txtServerName.Text = $srvName + Write-Log "Selected server: $srvName" + + # Check cached connection state for this server + $connState = $script:ServerConnectionStates[$srvName] + if ($null -ne $connState -and $connState.Success) { + # Server is connected: show its version/details in the DataGrid + $dgServerInfo.ItemsSource = @([PSCustomObject]$connState | Select-Object Instance, Version, Edition, Status) + } else { + # Server is not connected: clear the DataGrid so no old server details are visible + $dgServerInfo.ItemsSource = $null + } } elseif ($selectedItem.Tag -is [System.Management.Automation.PSCustomObject]) { # It's a database node $db = $selectedItem.Tag Write-Log "Selected database: $($db.Name) (Size: $($db.Size), Status: $($db.Status))" + + # Show database details in the main information grid + $dgServerInfo.ItemsSource = @($db | Select-Object Name, Status, Size, RecoveryModel) + + # Also, update txtServerName with the parent server's tag so they see which server it belongs to + $parentItem = $selectedItem.Parent + if ($null -ne $parentItem -and $parentItem.Tag -is [string]) { + $txtServerName.Text = $parentItem.Tag + } } } else { # Hide detail panel if selection is null or ROOT $gridDetails.Visibility = "Collapsed" + $dgServerInfo.ItemsSource = $null } }) @@ -231,11 +255,17 @@ $btnConnect.Add_Click({ if ($result.Success) { Write-Log "Connected successfully to $($result.Instance)!" $dgServerInfo.ItemsSource = @([PSCustomObject]$result | Select-Object Instance, Version, Edition, Status) + # Cache successful connection state + $script:ServerConnectionStates[$server] = $result } else { Write-Log "Connection failed: $($result.Error)" + $script:ServerConnectionStates[$server] = $null + $dgServerInfo.ItemsSource = $null } } catch { Write-Log "Critical Error: $($_.Exception.Message)" + $script:ServerConnectionStates[$server] = $null + $dgServerInfo.ItemsSource = $null } finally { $btnConnect.IsEnabled = $true }