by Lembasts at 2012-12-11 17:59:09
Greetings,by nohandle at 2012-12-12 02:05:25
I have a script that pulls data from a SQL database.
One of the properties is defined as an integer when I do a GM.
I can use the -eq operator, but when I use -lt or -gt I get an error :
Cannot compare “” because it is not IComparable.
What on earth does this mean please?
Ignore me…its because the field does not contain a number.
Interesting though, when I display it, it looks blank but it does not equal “” and does not equal $null. The length of the field is 1. I wonder what is in this field? I notice this happens with all SQL fields that are supposedly null.
[quote=“Lembasts”]Cannot compare “” because it is not IComparable.[/quote]by Lembasts at 2012-12-13 14:49:58
I means it does not implement this interface I suppose:
http://msdn.microsoft.com/en-us/library … rable.aspx
[quote=“Lembasts”]Ignore me…its because the field does not contain a number. [/quote]
What is the exact type of the value?
or the gm way.
The gm shows the variable is an integer.by nohandle at 2012-12-13 15:06:16
The exact type of the value is “System.DBNull”.
So how can I test for that type of null value in an if statement?
$variable -is [System.DBNull] I think.by Lembasts at 2012-12-13 16:18:48
[quote=“nohandle”]$variable -is [System.DBNull] I think.[/quote]by nohandle at 2012-12-14 00:52:03
I got a false result when i ran the above.
to comply with the syntax of the GetType example the previous one should read:by nohandle at 2012-12-14 01:16:26
$variable.property -is [System.DBNull]
Did you specified the property when you tried it? I should have described it more precisely the first time, sorry.
by Lembasts at 2012-12-17 13:11:30
$dbNull= [System.DBNull]]unfortuntely unlike normal null dbNull results in True when casted to bool
just like most of the others values does (0, $null and of course False does not)
so you can’t use it in condition directly to learn if the variable has value
that is not dbNull.
Falseto check if the value is dbNull you have to check if it is of the dbNull type
$dbNull -is [System.DBNull]you can also use the type without the System because it is loaded by default
$dbNull -is [DBNull]
When i run this:by nohandle at 2012-12-18 03:14:36
The answer is System.DBNull.
When I then run this:
$sqlqr.lastmessagestateid.gettype().fullname -is [DBNull]
the answer is false.
[quote=“Lembasts”]by Lembasts at 2012-12-18 12:37:11
this tells you that the type of the original item is DBNull but it outputs the answer as (type) string so if you do [quote=“Lembasts”]
$sqlqr.lastmessagestateid.gettype().fullname -is [DBNull][/quote] you ask if the answer returned is of type dbNull which obvously is not.
You have to ask if the original item is of type dbNull:
$sqlqr.lastmessagestateid -is [DBNull]
Thats embarrasingby nohandle at 2012-12-18 12:56:31
I finally got True with your last statement!
No it’s not, that is learning
Please mark the thread as solved (by the solved button) if your issue got solved.