Null Date Field

Its an old problem. Even in VCL, a datefield that is null gets a date of 1899-12-30. Null is a valid value and should be displayed as blank.

In VCL, I had to override a few things to do this. Suggestions for where I should do this for XData???

I can think of some.

  1. Have a calculated field (type string) for display. I could put this in the DB or in the entities.
  2. Override some Get method in the TDataset
  3. Override some Get method in TDateField
  4. Override the DateTimePicker (or subclass it). Not sure how that interacts with HTML's input.

A date of value equals to 0 is 1899-12-31. If you want a null value, save null in the database, not 0. Use NullableDateTime type for that.

About XData, it depends on where you want to change it. You mentioned only datasets and data-aware controls. That's not XData, and you should handle it the same way you do in VCL applications.

In TwebDbgrid a date with value NULL is displayed as 1899-12-30.. how can make that cell empty?

Implement column.OnGetCellData with something like:

procedure TForm2.WebDBGrid1GetCellData(Sender: TObject; ACol, ARow: Integer;
  AField: TField; var AValue: string);
begin
  if (ACol = DateColumn) then
  begin
    if AField.AsDateTime = 0 then
      AValue := '';
  end;
end;

I had started using this solution in case it works better for others.

  • I declared the dates as Nullable in the Entity definition.

  • In the List View (Grid), I am using Calculated Fields like this

    procedure Tfrm_Staff_List.xds_CalcFields(DataSet: TDataSet);
   begin
     inherited;
     xds_SStarted.Text  := Nullable_Date (xds_Started);
     xds_SDeparted.Text := Nullable_Date (xds_Departed);
   end; 

   function Nullable_Date (AArg : TDateTimeField) : string;
   begin
     if AArg.IsNull
     then Result := ''
     else Result := DateToStr (AArg.Value);
   end;
  • In the Edit Form on the Edit control it works fine because of nullable declaration.