how to check if null value in single line
ado, delphi, delphi-xe2
Solution
That is the common way to go:
Extract the repeating parts to keep the code DRY and to increase readability
function IfNull( const Value, Default : OleVariant ) : OleVariant;
begin
if Value = NULL then
Result := Default
else
Result := Value;
end;
...
OneSpell.Plus := IfNull( FQuery.Recordset.Fields[ DB_FLD_PER_CENT].Value, 0 );
Problem
I am getting a value from db, and when the value is NULL I get an error Could not convert variant of type (null) into type (Integer) If I fill in the db with 0 instead of nothing (NULL) the error goes away So say I have this ``` OneSpell.PerCent := FQuery.Recordset.Fields[ DB_FLD_PER_CENT ].Value; OneSpell.Plus := TCardPlus ( FQuery.Recordset.Fields[ DB_FLD_PLUS ].Value ); OneSpell.Quantity := FQuery.Recordset.Fields[ DB_FLD_QUANTITY ].Value; ``` is there a way to say if the value is NULL then make it 0? Or does this have to be done via the db. `FQuery` is an `adoquery` and db is access I know I could do ``` if .... = null then onespell.plus := 0 else ......... ``` But I want to do it in one line for each value of onespell