EVariantTypeCastError Exception in DBGrids lazarus / 16-Databases ---------------------------------------------------------------------- Vodnik · 2018-04-10 14:18 EVariantTypeCastError Exception in DBGrids with message: "Could not convert variant of type (Null) into type (Date)" This occurs in TCustomDBGrid.InternalAutoSizeColumn, line 33: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TCustomDBGrid.InternalAutoSizeColumn(aCol: Integer; aCanvas: TCanvas; aDatalinkActive: Boolean);var  Field: TField;  C: TGridColumn;  ColWidth: Integer;  ARow,w: Integer;  s: string; begin  Field := GetFieldFromGridColumn(ACol);  C := ColumnFromGridColumn(ACol);   if (C<>nil) and (C.Title<>nil) then begin    aCanvas.Font := C.Title.Font;    ColWidth := aCanvas.TextWidth(trim(C.Title.Caption));    aCanvas.Font := C.Font;  end else begin    if (Field<>nil) then begin      aCanvas.Font := TitleFont;      ColWidth := aCanvas.TextWidth(Field.FieldName);    end    else      ColWidth := 0;    aCanvas.Font := Font;  end;   if (Field<>nil) and aDatalinkActive then    for ARow := FixedRows to RowCount-1 do begin       FDatalink.ActiveRecord := ARow - FixedRows;       if Field.dataType<>ftBlob then         s := trim(Field.DisplayText)      else        s := '(blob)';      w := aCanvas.TextWidth(s);      if w>ColWidth then        ColWidth := w;   end;   if ColWidth=0 then    ColWidth := GetColumnWidth(ACol);   ColWidths[ACol] := ColWidth + 15;end;  Lazarus 1.8.0, FPC 3.0.4 I use TODBCconnection to Informix. I have tried my application on several different DB's, only one reports this error. This may be because of error in DB. I'm not feeling brave enough to do proper correction in DBGrids source code. Please help me to filter out this exception. ---------------------------------------------------------------------- Vodnik · 2018-04-11 20:10 Well, non-trivial (for me) troubleshooting have brought me to the problem cause: it was in my TSQLQuery.onGetText event handler. Corrected variant:  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TDBForm.DBquery1callendGetText(Sender: TField; var aText: string; DisplayText: Boolean);begin  if Sender.IsNull then aText:='Null' else    if (DisplayText) then      aText := FormatDateTime('hh:nn:ss', Sender.Value / SecsPerDay)    else aText := 'Unknown';end;  Sendler.IsNull check is mandatory!!! What is really strange, that this event handler is called even if query returns no data.