Pass a value from winform

Welcome Forums General PowerShell Q&A Pass a value from winform

Viewing 0 reply threads
  • Author
    Posts
    • #6054
      Participant
      Topics: 1562
      Replies: 0
      Points: 1
      Rank: Member

      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&#40;&quot;System.Drawing&quot;&#41; <br />[void] [System.Reflection.Assembly]::LoadWithPartialName&#40;&quot;System.Windows.Forms&quot;&#41; </p><p>$objForm = New-Object System.Windows.Forms.Form <br />$objForm.Text = &quot;Data Entry Form&quot;<br />$objForm.Size = New-Object System.Drawing.Size&#40;300,200&#41; <br />$objForm.StartPosition = &quot;CenterScreen&quot;</p><p>$objForm.KeyPreview = $True<br />$objForm.Add_KeyDown&#40;{if &#40;$_.KeyCode -eq &quot;Enter&quot;&#41; <br />    {$from=$objTextBox.Text;$objForm.Close&#40;&#41;}}&#41;<br />$objForm.Add_KeyDown&#40;{if &#40;$_.KeyCode -eq &quot;Escape&quot;&#41; <br />    {$objForm.Close&#40;&#41;}}&#41;</p><p>$OKButton = New-Object System.Windows.Forms.Button<br />$OKButton.Location = New-Object System.Drawing.Size&#40;75,120&#41;<br />$OKButton.Size = New-Object System.Drawing.Size&#40;75,23&#41;<br />$OKButton.Text = &quot;OK&quot;<br />$OKButton.Add_Click&#40;{$from=$objTextBox.Text;$objForm.Close&#40;&#41;}&#41;<br />$objForm.Controls.Add&#40;$OKButton&#41;</p><p>$CancelButton = New-Object System.Windows.Forms.Button<br />$CancelButton.Location = New-Object System.Drawing.Size&#40;150,120&#41;<br />$CancelButton.Size = New-Object System.Drawing.Size&#40;75,23&#41;<br />$CancelButton.Text = &quot;Cancel&quot;<br />$CancelButton.Add_Click&#40;{$objForm.Close&#40;&#41;}&#41;<br />$objForm.Controls.Add&#40;$CancelButton&#41;</p><p>$objLabel = New-Object System.Windows.Forms.Label<br />$objLabel.Location = New-Object System.Drawing.Size&#40;10,20&#41; <br />$objLabel.Size = New-Object System.Drawing.Size&#40;280,20&#41; <br />$objLabel.Text = &quot;From Date &#40;YYYY-MM-DD&#41;:&quot;<br />$objForm.Controls.Add&#40;$objLabel&#41; </p><p>$objTextBox = New-Object System.Windows.Forms.TextBox <br />$objTextBox.Location = New-Object System.Drawing.Size&#40;10,40&#41; <br />$objTextBox.Size = New-Object System.Drawing.Size&#40;260,20&#41; <br />$objForm.Controls.Add&#40;$objTextBox&#41; </p><p>$objForm.Topmost = $True</p><p>$objForm.Add_Shown&#40;{$objForm.Activate&#40;&#41;}&#41;<br />[void] $objForm.ShowDialog&#40;&#41;</p><p>$from</p><p>[void] [System.Reflection.Assembly]::LoadWithPartialName&#40;&quot;System.Drawing&quot;&#41; <br />[void] [System.Reflection.Assembly]::LoadWithPartialName&#40;&quot;System.Windows.Forms&quot;&#41; </p><p>$objForm = New-Object System.Windows.Forms.Form <br />$objForm.Text = &quot;Data Entry Form&quot;<br />$objForm.Size = New-Object System.Drawing.Size&#40;300,200&#41; <br />$objForm.StartPosition = &quot;CenterScreen&quot;</p><p>$objForm.KeyPreview = $True<br />$objForm.Add_KeyDown&#40;{if &#40;$_.KeyCode -eq &quot;Enter&quot;&#41; <br />    {$to=$objTextBox.Text;$objForm.Close&#40;&#41;}}&#41;<br />$objForm.Add_KeyDown&#40;{if &#40;$_.KeyCode -eq &quot;Escape&quot;&#41; <br />    {$objForm.Close&#40;&#41;}}&#41;</p><p>$OKButton = New-Object System.Windows.Forms.Button<br />$OKButton.Location = New-Object System.Drawing.Size&#40;75,120&#41;<br />$OKButton.Size = New-Object System.Drawing.Size&#40;75,23&#41;<br />$OKButton.Text = &quot;OK&quot;<br />$OKButton.Add_Click&#40;{$to=$objTextBox.Text;$objForm.Close&#40;&#41;}&#41;<br />$objForm.Controls.Add&#40;$OKButton&#41;</p><p>$CancelButton = New-Object System.Windows.Forms.Button<br />$CancelButton.Location = New-Object System.Drawing.Size&#40;150,120&#41;<br />$CancelButton.Size = New-Object System.Drawing.Size&#40;75,23&#41;<br />$CancelButton.Text = &quot;Cancel&quot;<br />$CancelButton.Add_Click&#40;{$objForm.Close&#40;&#41;}&#41;<br />$objForm.Controls.Add&#40;$CancelButton&#41;</p><p>$objLabel = New-Object System.Windows.Forms.Label<br />$objLabel.Location = New-Object System.Drawing.Size&#40;10,20&#41; <br />$objLabel.Size = New-Object System.Drawing.Size&#40;280,20&#41; <br />$objLabel.Text = &quot;To Date &#40;YYYY-MM-DD&#41;&quot;<br />$objForm.Controls.Add&#40;$objLabel&#41; </p><p>$objTextBox = New-Object System.Windows.Forms.TextBox <br />$objTextBox.Location = New-Object System.Drawing.Size&#40;10,40&#41; <br />$objTextBox.Size = New-Object System.Drawing.Size&#40;260,20&#41; <br />$objForm.Controls.Add&#40;$objTextBox&#41; </p><p>$objForm.Topmost = $True</p><p>$objForm.Add_Shown&#40;{$objForm.Activate&#40;&#41;}&#41;<br />[void] $objForm.ShowDialog&#40;&#41;</p><p>$to</p><p>$SqlConnection = New-Object System.Data.SqlClient.SqlConnection<br />$SqlConnection.ConnectionString = &quot;Server=10.10.1.248;Database=AMSNewDataWarehouse;User id=YQBGantt;Password=123yqbgantt&quot;<br />$SqlCmd = New-Object System.Data.SqlClient.SqlCommand<br />$SqlCmd.Connection = $SqlConnection<br />$SqlCmd.CommandText = &quot;YQBreport2&quot;<br />$SqlCmd.CommandType = [System.Data.CommandType]::StoredProcedure<br />$param1=$sqlcmd.Parameters.Add&#40;&quot;@from&quot; , [System.Data.SqlDbType]::DateTime, $null&#41;<br />$param1.Value=Get-Date $from -format &quot;yyyy-MM-dd HH:mm:ss.fff&quot;<br />$param2=$sqlcmd.Parameters.Add&#40;&quot;@to&quot;, [System.Data.SqlDbType]::DateTime, $null&#41;<br />$param2.Value=Get-Date $to -format &quot;yyyy-MM-dd HH:mm:ss.fff&quot;<br />$SqlConnection.Open&#40;&#41;<br />$sqlcmd.ExecuteNonQuery&#40;&#41;<br />$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter<br />$SqlAdapter.SelectCommand = $SqlCmd<br />$DataSet = New-Object System.Data.DataSet<br />$SqlAdapter.Fill&#40;$DataSet&#41;<br />$SqlConnection.Close&#40;&#41;<br />$SQLResult =$DataSet.Tables[0]<br />$commands = $SQLResult | foreach-object -process { $_.output }&gt; output.ps1<br />.\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
      <br />Name                           Value<br />----                           -----<br />CLRVersion                     2&#46;0&#46;50727&#46;5466<br />BuildVersion                   6&#46;1&#46;7601&#46;17514<br />PSVersion                      2&#46;0<br />WSManStackVersion              2&#46;0<br />PSCompatibleVersions           {1&#46;0, 2&#46;0}<br />SerializationVersion           1&#46;1&#46;0&#46;1<br />PSRemotingProtocolVersion      2&#46;1<br />

      Windows 2008 R2

      <br />Name                           Value<br />----                           -----<br />CLRVersion                     2&#46;0&#46;50727&#46;5420<br />BuildVersion                   6&#46;1&#46;7601&#46;17514<br />PSVersion                      2&#46;0<br />WSManStackVersion              2&#46;0<br />PSCompatibleVersions           {1&#46;0, 2&#46;0}<br />SerializationVersion           1&#46;1&#46;0&#46;1<br />PSRemotingProtocolVersion      2&#46;1<br />

      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!

Viewing 0 reply threads
  • The topic ‘Pass a value from winform’ is closed to new replies.