January 1, 2012 at 12:00 am #5340
by Lembasts at 2012-12-11 17:59:09
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.
by nohandle at 2012-12-12 02:05:25
[quote="Lembasts"]Cannot compare "" because it is not IComparable.[/quote]
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.
by Lembasts at 2012-12-13 14:49:58
The gm shows the variable is an integer.
The exact type of the value is "System.DBNull".
So how can I test for that type of null value in an if statement?
by nohandle at 2012-12-13 15:06:16
$variable -is [System.DBNull] I think.
by Lembasts at 2012-12-13 16:18:48
[quote="nohandle"]$variable -is [System.DBNull] I think.[/quote]
I got a false result when i ran the above.
by nohandle at 2012-12-14 00:52:03
to comply with the syntax of the GetType example the previous one should read:
$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 nohandle at 2012-12-14 01:16:26
$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]
by Lembasts at 2012-12-17 13:11:30
When i run this:
The answer is System.DBNull.
When I then run this:
$sqlqr.lastmessagestateid.gettype().fullname -is [DBNull]
the answer is false.
by nohandle at 2012-12-18 03:14:36
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]
by Lembasts at 2012-12-18 12:37:11
Thats embarrasing 🙂
I finally got True with your last statement!
by nohandle at 2012-12-18 12:56:31
No it's not, that is learning 🙂
Please mark the thread as solved (by the solved button) if your issue got solved.
You must be logged in to reply to this topic.