What is the value returned by AsString when FieldByName('fieldname') has null value?

delphi

Solution

You can test this yourself simply by calling

MyDataSet.FieldByName('MyField').Clear

That sets MyField to `Null` and, after that, calling `AsString` on it returns an empty (zero-length) string.

The `GetAsString` method of TField descendants typically contain code like this:

function TIntegerField.GetAsString: string;
var
  L: Longint;
begin
  if GetValue(L) then Str(L, Result) else Result := '';
end;

Here, `GetValue` succeeds if it is possible to retrieve a value from the current record buffer. If it fails, the field is taken to contain `Null`.

Problem

What value does `val`, which is of string type, get when the column named `fieldName` is `null` for the selected row? Here `myQry` is a database query. ``` val := myQry.FieldByName('fieldName').AsString ``` Here column `fieldName` does exist in the table, but for the selected row/record, the value is `null`. I have looked here. It was informative, but didn't have the information I needed I am afraid. I also looked at this but that didn't help much either.

Original source

Related problems