Question

I have a following firebird dataset:

ds1 : TpFIBDataset;

DFM file:

object ds1ID: TFIBIntegerField
 FieldName = 'ID'
end
object ds1FIELD_VALUE: TFIBStringField
  FieldName = 'FIELD_VALUE'
  Size = 250
end

In my Firebird database: ID is integer field and FIELD_VALUE is varchar field.

Delphi 7: Data is being inserted like this

ds1.InsertRecord([123, anyValue]);

here anyValue : variant;

If anyValue = null, null is getting inserted into the database which is the required functionality.

In Delphi XE4, I am doing same like this:

ds1.Insert;
ds1.FieldByName('ID').AsInteger := 123;
ds1.FieldByName('FIELD_VALUE').AsString := anyValue;
ds1.Post;

I get error: could not convert variant of type (null) into type (OleStr)

When I try like this

ds1.Insert;
ds1.FieldByName('ID').AsInteger := 123;
ds1.FieldByName('FIELD_VALUE').AsString := VarToStr(anyValue);
ds1.Post;

Empty string is inserted into the database, but I need null there. What and where should I make change to accomplish this task.

Was it helpful?

Solution

You need use

ds1.FieldByName('FIELD_VALUE').Value := anyValue;

instead of

ds1.FieldByName('FIELD_VALUE').AsString := anyValue;

There is some difference in this delphi versions. Starting from delphi2006 there is another memory manager that works more precise. In example in delphi 7 and lower you can assign NULL to string or integer value. And comparision NULL with 0 will return true. In greater versions you can't do this. Null is not equal 0 or empty string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top