by apriljuly at 2013-03-12 05:49:06
Hi,by SalvaG at 2013-03-12 06:37:05
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
Perhaps .NET framework versions are different…by apriljuly at 2013-03-12 06:47:47
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
Hi @SalvaG Which version of .Net you are running onby SalvaG at 2013-03-13 00:42:44
Run $PSVersionTableby apriljuly at 2013-03-13 03:35:40
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
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.by SalvaG at 2013-03-13 04:11:19
Do you know how to force the code to run on powershell v2?
Use the -version parameterby apriljuly at 2013-03-13 05:15:50
powershell.exe -version 2 .\myScript.ps1
More info at http://technet.microsoft.com/en-us/library/hh847736.aspx
It is a powershell version issue, and finally got it work with adding script: in front of front.by SalvaG at 2013-03-13 05:45:10
$script:from = $objTextBox.Text
I’m glad to hear it.
Please change the subject to [solved]
Thanks!