Board index » delphi » Invalid variant type conversion - ADO and Delphi

Invalid variant type conversion - ADO and Delphi

From www.Borland.com/techvoyage/articles/ADOBasics/ADOBasics.html

Charlie Calvert writes a procedure similar to the one below:

procedure TForm1.VariantBtnClick(Sender: TObject);
var
  RecordSet, NewConnection: OleVariant;
  Y, i: Integer;
begin
  NewConnection:=CreateOleObject('ADODB.Connection');

NewConnection.Open('DSN=odbc_atis;UID=ntrider;PWD=utility;','ntrider','utili
ty',0);
  // Create an empty recordset object
  RecordSet := CreateOleObject('ADODB.Recordset');

  // Fill the recordset
  RecordSet.Open(SELECTSTRING, NewConnection{DSNSTRING});

  // Display the data
  Y := 1;
  repeat
    StringGrid1.RowCount:=Y+1;
    Application.ProcessMessages;
    for i := 0 to 3 do
    begin

{999}      StringGrid1.Cells[i, Y] :=  RecordSet.Fields[i].Value;

    end;
    RecordSet.Move(1);
    Inc(Y);
    Edit2.Text:=IntToStr(StringGrid1.RowCount-1);
  until RecordSet.EOF;
end;

When I recieve a null from the database at line {999} I get an "Invalid
Variant type conversion" error.

Could someone please suggest how I may handle this situation.

Vlad Gonchar

 

Re:Invalid variant type conversion - ADO and Delphi


Delphi Null variant values don't convert automatically to empty strings. You
will need to test like this:

if (RecordSet.Fields [].Value = Null) then
  Cells [x, y] := ''
else
  Cells [x, y] := RecordSet.Fields [].Value;

have fun
--
Binh Ly
http://www.castle.net/~bly/Programming/Delphi

Quote
vlad gochar <vlad.gonc...@city.mississauga.on.ca> wrote in message

news:7nkaoq$57f2@forums.borland.com...
Quote
> From www.Borland.com/techvoyage/articles/ADOBasics/ADOBasics.html

> Charlie Calvert writes a procedure similar to the one below:

> procedure TForm1.VariantBtnClick(Sender: TObject);
> var
>   RecordSet, NewConnection: OleVariant;
>   Y, i: Integer;
> begin
>   NewConnection:=CreateOleObject('ADODB.Connection');

NewConnection.Open('DSN=odbc_atis;UID=ntrider;PWD=utility;','ntrider','utili
Quote
> ty',0);
>   // Create an empty recordset object
>   RecordSet := CreateOleObject('ADODB.Recordset');

>   // Fill the recordset
>   RecordSet.Open(SELECTSTRING, NewConnection{DSNSTRING});

>   // Display the data
>   Y := 1;
>   repeat
>     StringGrid1.RowCount:=Y+1;
>     Application.ProcessMessages;
>     for i := 0 to 3 do
>     begin

> {999}      StringGrid1.Cells[i, Y] :=  RecordSet.Fields[i].Value;

>     end;
>     RecordSet.Move(1);
>     Inc(Y);
>     Edit2.Text:=IntToStr(StringGrid1.RowCount-1);
>   until RecordSet.EOF;
> end;

> When I recieve a null from the database at line {999} I get an "Invalid
> Variant type conversion" error.

> Could someone please suggest how I may handle this situation.

> Vlad Gonchar

Re:Invalid variant type conversion - ADO and Delphi


You can also use the
TRY
   your code
EXCEPT
   your error handling
END
block.

Enjoy !!!

  vcard.vcf
< 1K Download

Other Threads