Recent

Author Topic: EVariantTypeCastError Exception in DBGrids  (Read 1924 times)

Vodnik

  • Full Member
  • ***
  • Posts: 210
EVariantTypeCastError Exception in DBGrids
« on: April 10, 2018, 04:18:32 pm »
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][+][-]
  1. procedure TCustomDBGrid.InternalAutoSizeColumn(aCol: Integer; aCanvas: TCanvas; aDatalinkActive: Boolean);
  2. var
  3.   Field: TField;
  4.   C: TGridColumn;
  5.   ColWidth: Integer;
  6.   ARow,w: Integer;
  7.   s: string;
  8.  
  9. begin
  10.   Field := GetFieldFromGridColumn(ACol);
  11.   C := ColumnFromGridColumn(ACol);
  12.  
  13.   if (C<>nil) and (C.Title<>nil) then begin
  14.     aCanvas.Font := C.Title.Font;
  15.     ColWidth := aCanvas.TextWidth(trim(C.Title.Caption));
  16.     aCanvas.Font := C.Font;
  17.   end else begin
  18.     if (Field<>nil) then begin
  19.       aCanvas.Font := TitleFont;
  20.       ColWidth := aCanvas.TextWidth(Field.FieldName);
  21.     end
  22.     else
  23.       ColWidth := 0;
  24.     aCanvas.Font := Font;
  25.   end;
  26.  
  27.   if (Field<>nil) and aDatalinkActive then
  28.     for ARow := FixedRows to RowCount-1 do begin
  29.  
  30.       FDatalink.ActiveRecord := ARow - FixedRows;
  31.  
  32.       if Field.dataType<>ftBlob then
  33.          s := trim(Field.DisplayText)
  34.       else
  35.         s := '(blob)';
  36.       w := aCanvas.TextWidth(s);
  37.       if w>ColWidth then
  38.         ColWidth := w;
  39.  
  40.     end;
  41.  
  42.   if ColWidth=0 then
  43.     ColWidth := GetColumnWidth(ACol);
  44.  
  45.   ColWidths[ACol] := ColWidth + 15;
  46. end;
  47.  

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

  • Full Member
  • ***
  • Posts: 210
Re: EVariantTypeCastError Exception in DBGrids
« Reply #1 on: April 11, 2018, 10:10:32 pm »
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][+][-]
  1. procedure TDBForm.DBquery1callendGetText(Sender: TField; var aText: string; DisplayText: Boolean);
  2. begin
  3.   if Sender.IsNull then aText:='Null' else
  4.     if (DisplayText) then
  5.       aText := FormatDateTime('hh:nn:ss', Sender.Value / SecsPerDay)
  6.     else aText := 'Unknown';
  7. end;
  8.  

Sendler.IsNull check is mandatory!!!

What is really strange, that this event handler is called even if query returns no data.
« Last Edit: April 11, 2018, 10:16:18 pm by Vodnik »

 

TinyPortal © 2005-2018