If statement resolves as true but doesn't run

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
randall.faulk
Posts: 2
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Thu May 10, 2018 12:46 pm

If statement resolves as true but doesn't run

Post by randall.faulk »

Powershell Studio 64-bit
Windows10 64-bit

I am running a script that I have used before and it worked fine in another form. I've copied it to a new form in a new project and it is failing to run. No errors appear. I can take the script and run it in Powershell_ISE and it resolves just fine. I am stumped as to why it is just failing to run.
Here is the if statement
if (($strike1 -eq "") -and ($strike2 -eq "") -and ($strike3 -eq ""))

I am querying an SQL DB to get the information. If the strike field is empty or null then the code would run.

I have also tried
if (($strike1 -eq $null) -and ($strike2 -eq $null) -and ($strike3 -eq $null))
and
if (($strike1.length -eq 0) -and ($strike2.length -eq 0) -and ($strike3.length -eq 0))

everything fails to resolve as true and run properly.

User avatar
Alexander Riedel
Posts: 7376
Joined: Tue May 29, 2007 4:43 pm

Re: If statement resolves as true but doesn't run

Post by Alexander Riedel »

We need a little more context. Where exactly is that code? What indication do you have that the code triggered by the if statement does not execute?
When you debug, do you hit a breakpoint at the if statement?
Alexander Riedel
SAPIEN Technologies, Inc.

jvierra
Posts: 14444
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: If statement resolves as true but doesn't run

Post by jvierra »

SQLFields do not resolve to null or empty. They either are checked with "DBNull" or by testing the "Value" member of the field. My best guess uis that you have assigned a field to the variable. This doesn't assign the contents it makes the variable a pointer to the field object.

[System.DBNull]::Value

jvierra
Posts: 14444
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: If statement resolves as true but doesn't run

Post by jvierra »

To test a field for null we would do the following:

$dt.ROws[0]['autoval'].Value -eq [system.dbnull].Value

Or this is easier:

if($dt.Rows[0]['autoval'].Value){

This will automatically detect nulls in data fields. To check multiple fields:

if($dt.Rows[0]['field1'].Value -and $dt.Rows[0]['field2'].Value -and $dt.Rows[0]['field3'].Value){

If you do this:

$f1 = $dt.Rows[0]['field1']

Then you have to do this to test the variable:

$f1.Value

To quickly test multiple fields then:

$f1.Value -and $f2.Value -and $f3.Value

It is important to remember that assignments of object properties that are objects and not ValueTypes create variables that are reference variables. "[ref]" or reference types.

User avatar
randall.faulk
Posts: 2
Joined: Thu May 10, 2018 12:46 pm

Re: If statement resolves as true but doesn't run

Post by randall.faulk »

Hello all. It was the DBNull that was getting me. I have powershell looking for it now and everything is working. Thank you for the help!

Locked