Hi,
I have a script to try and complete the objectives below:
1. Enter a username into a form field
2. Have "Find" button search a SQL database for the user and its information
3. Store the user information in a variable
4. Output some of the user information in the same form's fields
When I click the "Find" button the result(s) are not displayed in the field but will display after the form is closed out. The code is below. Can someone help me make this work?
#Function
#**********************************************************************************************\
Function Get-UserInfoFromUsersTable ([string]$u){
$UserName = $u
$SQLServer = "SomeDatabase"
$SQLDBName = "UserDirectory"
$SqlQuery = "SELECT a.UserName,
a.FirstName,
a.LastName,
a.DisplayName,
a.JobTitle,
a.CostCenter,
b.[Description] AS 'EmployeeType',
c.FirstName AS 'SupervisorFirstName',
c.LastName AS 'SupervisorLastName'
FROM dbo.PromegaUsers a
JOIN dbo.EmployeeType b ON a.EmployeeType = b.EmployeeType
JOIN dbo.PromegaUsers c ON a.SupervisorID = c.UserId
WHERE a.UserName = '$UserName'"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$DataSet.Tables[0]
}
#Script to input user info into useful variables
#-----------------------------------------------
#Input the function and its values into a variable
$test = Get-UserInfoFromUsersTable ($objTextBoxFName.Text)
$UName = $test.UserName
$FName = $test.FirstName
$LName = $test.LastName
$DName = $test.DisplayName
$JTitle = $test.JobTitle
# Form Setup
#*******************************************************************************************\
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Find User Info"
$objForm.Size = New-Object System.Drawing.Size(300,300)
$objForm.StartPosition = "CenterScreen"
$objForm.AutoSize = $False
$objForm.BackColor = "LightSkyBlue"
$objForm.KeyPreview = $True
#$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
#{$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
#*******************************************************************************************\
# Button & text box Setup
#*******************************************************************************************\
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(65,40)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "Find"
$OKButton.backcolor = [System.Drawing.Color]::Azure
#Adds the user info value to the result box
$OKButton.Add_Click({$objTextBoxResult.Text = $JTitle})
$objForm.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,40)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.backcolor = [System.Drawing.Color]::Azure
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Size(10,80)
$objLabel1.Size = New-Object System.Drawing.Size(280,20)
$objLabel1.Text = "First Name:"
$objForm.Controls.Add($objLabel1)
$objTextBoxFName = New-Object System.Windows.Forms.TextBox
$objTextBoxFName.Location = New-Object System.Drawing.Size(10,100)
$objTextBoxFName.Size = New-Object System.Drawing.Size(260,20)
$objTextBoxFName.ReadOnly = $False
$objForm.Controls.Add($objTextBoxFName)
$objLabel2 = New-Object System.Windows.Forms.Label
$objLabel2.Location = New-Object System.Drawing.Size(10,130)
$objLabel2.Size = New-Object System.Drawing.Size(280,20)
$objLabel2.Text = "Last Name:"
$objForm.Controls.Add($objLabel2)
$objTextBoxLName = New-Object System.Windows.Forms.TextBox
$objTextBoxLName.Location = New-Object System.Drawing.Size(10,150)
$objTextBoxLName.Size = New-Object System.Drawing.Size(260,20)
$objTextBoxLName.ReadOnly = $False
$objForm.Controls.Add($objTextBoxLName)
$objLabel3 = New-Object System.Windows.Forms.Label
$objLabel3.Location = New-Object System.Drawing.Size(10,180)
$objLabel3.Size = New-Object System.Drawing.Size(280,20)
$objLabel3.Text = "Result:"
$objForm.Controls.Add($objLabel3)
$objTextBoxResult = New-Object System.Windows.Forms.TextBox
$objTextBoxResult.Location = New-Object System.Drawing.Size(10,200)
$objTextBoxResult.Size = New-Object System.Drawing.Size(260,20)
$objTextBoxResult.ReadOnly = $True
$objForm.Controls.Add($objTextBoxResult)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$x
#**********************************************************************************************\