Pass a value from winform

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 11 months ago.

  • Author
    Posts
  • #6054

    by apriljuly at 2013-03-12 05:49:06

    Hi,

    It's a really weird case to me. I hope someone can help.

    I created 2 input box using winform to input $from and $to, and then convert the string to datetime to pass to an SQL stored procedure. Weird thing is, this piece of code works perfectly on my own machine. However, when I tried it on another machine(basically is a server located in Canada), I'm using remote desktop to access it. It's not working anymore. And it seems like whatever I input in the box, does not get pass on...I tried to output $from and it's nothing there.

    I will get error Get-Date: Cannot bind parameter 'date' to the target. Exception setting "Date": "Object reference not set to an instance of an object." Can anyone tell me why it's working on one machine and not the other? what is missing? Is it because of the OS is different on 2 machine? one is win7, one is windows server 2008.

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

    $objForm = New-Object System.Windows.Forms.Form
    $objForm.Text = "Data Entry Form"
    $objForm.Size = New-Object System.Drawing.Size(300,200)
    $objForm.StartPosition = "CenterScreen"

    $objForm.KeyPreview = $True
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
    {$from=$objTextBox.Text;$objForm.Close()}})
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
    {$objForm.Close()}})

    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(75,120)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"
    $OKButton.Add_Click({$from=$objTextBox.Text;$objForm.Close()})
    $objForm.Controls.Add($OKButton)

    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(150,120)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = "Cancel"
    $CancelButton.Add_Click({$objForm.Close()})
    $objForm.Controls.Add($CancelButton)

    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(10,20)
    $objLabel.Size = New-Object System.Drawing.Size(280,20)
    $objLabel.Text = "From Date (YYYY-MM-DD):"
    $objForm.Controls.Add($objLabel)

    $objTextBox = New-Object System.Windows.Forms.TextBox
    $objTextBox.Location = New-Object System.Drawing.Size(10,40)
    $objTextBox.Size = New-Object System.Drawing.Size(260,20)
    $objForm.Controls.Add($objTextBox)

    $objForm.Topmost = $True

    $objForm.Add_Shown({$objForm.Activate()})
    [void] $objForm.ShowDialog()

    $from

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

    $objForm = New-Object System.Windows.Forms.Form
    $objForm.Text = "Data Entry Form"
    $objForm.Size = New-Object System.Drawing.Size(300,200)
    $objForm.StartPosition = "CenterScreen"

    $objForm.KeyPreview = $True
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
    {$to=$objTextBox.Text;$objForm.Close()}})
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
    {$objForm.Close()}})

    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(75,120)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"
    $OKButton.Add_Click({$to=$objTextBox.Text;$objForm.Close()})
    $objForm.Controls.Add($OKButton)

    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(150,120)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = "Cancel"
    $CancelButton.Add_Click({$objForm.Close()})
    $objForm.Controls.Add($CancelButton)

    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(10,20)
    $objLabel.Size = New-Object System.Drawing.Size(280,20)
    $objLabel.Text = "To Date (YYYY-MM-DD)"
    $objForm.Controls.Add($objLabel)

    $objTextBox = New-Object System.Windows.Forms.TextBox
    $objTextBox.Location = New-Object System.Drawing.Size(10,40)
    $objTextBox.Size = New-Object System.Drawing.Size(260,20)
    $objForm.Controls.Add($objTextBox)

    $objForm.Topmost = $True

    $objForm.Add_Shown({$objForm.Activate()})
    [void] $objForm.ShowDialog()

    $to

    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
    $SqlConnection.ConnectionString = "Server=10.10.1.248;Database=AMSNewDataWarehouse;User id=YQBGantt;Password=123yqbgantt"
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    $SqlCmd.Connection = $SqlConnection
    $SqlCmd.CommandText = "YQBreport2"
    $SqlCmd.CommandType = [System.Data.CommandType]::StoredProcedure
    $param1=$sqlcmd.Parameters.Add("@from" , [System.Data.SqlDbType]::DateTime, $null)
    $param1.Value=Get-Date $from -format "yyyy-MM-dd HH:mm:ss.fff"
    $param2=$sqlcmd.Parameters.Add("@to", [System.Data.SqlDbType]::DateTime, $null)
    $param2.Value=Get-Date $to -format "yyyy-MM-dd HH:mm:ss.fff"
    $SqlConnection.Open()
    $sqlcmd.ExecuteNonQuery()
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $SqlCmd
    $DataSet = New-Object System.Data.DataSet
    $SqlAdapter.Fill($DataSet)
    $SqlConnection.Close()
    $SQLResult =$DataSet.Tables[0]
    $commands = $SQLResult | foreach-object -process { $_.output }> output.ps1
    .\output.ps1

    by SalvaG at 2013-03-12 06:37:05

    Perhaps .NET framework versions are different...
    I've tried your script on one of my server (WS2008R2) and it works (commented from 103 to 112).

    Perhaps this will help you : ttp://stackoverflow.com/questions/20946 ... -4-runtime

    by apriljuly at 2013-03-12 06:47:47

    Hi @SalvaG Which version of .Net you are running on

    by SalvaG at 2013-03-13 00:42:44

    Run $PSVersionTable

    Windows 7

    Name Value
    ---- -----
    CLRVersion 2.0.50727.5466
    BuildVersion 6.1.7601.17514
    PSVersion 2.0
    WSManStackVersion 2.0
    PSCompatibleVersions {1.0, 2.0}
    SerializationVersion 1.1.0.1
    PSRemotingProtocolVersion 2.1

    Windows 2008 R2


    Name Value
    ---- -----
    CLRVersion 2.0.50727.5420
    BuildVersion 6.1.7601.17514
    PSVersion 2.0
    WSManStackVersion 2.0
    PSCompatibleVersions {1.0, 2.0}
    SerializationVersion 1.1.0.1
    PSRemotingProtocolVersion 2.1

    by apriljuly at 2013-03-13 03:35:40

    I think it's because of different powershell version, because what's on the server is 3.0, therefore I suspect that's the reason my code is not working.

    Do you know how to force the code to run on powershell v2?

    by SalvaG at 2013-03-13 04:11:19

    Use the -version parameter

    powershell.exe -version 2 .\myScript.ps1

    More info at http://technet.microsoft.com/en-us/library/hh847736.aspx

    by apriljuly at 2013-03-13 05:15:50

    It is a powershell version issue, and finally got it work with adding script: in front of front.
    $script:from = $objTextBox.Text

    by SalvaG at 2013-03-13 05:45:10

    I'm glad to hear it.
    Please change the subject to [solved] 🙂

    Thanks!

You must be logged in to reply to this topic.