Exception when assigning NULL

Discussion of open issues, suggestions and bugs regarding ODAC (Oracle Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
LeOS
Posts: 1
Joined: Thu 01 Oct 2020 15:37

Exception when assigning NULL

Post by LeOS » Fri 02 Oct 2020 11:03

Hello together,
we updated ODAC VCL 11.1.3 to 11.3.2 and got a problem assigning a result param of a function with the Value property to a string.
When the result is NULL it throws an EVariantTypeCastError "Could not convert variant of type (Null) into type (OleStr)".
If we use the AsString property everything is fine. We know that using AsString is always recommended, but we have old legacy code in many older projects, so we'd prefer not to change that, if possible.

Code: Select all

str := OraStoredProc1.ParamByName('RESULT').Value; // the RESULT is NULL
ODAC VCL 11.1.3 (and all versions before) assignes an empty string to str.
ODAC VCL 11.3.2 throws the EVariantTypeCastError

A small example:

ORACLE Code

Code: Select all

CREATE OR REPLACE FUNCTION FncTestODAC(p NUMBER) RETURN VARCHAR2 IS
BEGIN
  IF p = 0 THEN
    RETURN 'TEST';
  ELSIF p = 1 THEN
    RETURN NULL;
  END IF;
END;
Delphi Code

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
var
  str : string;
begin
  OraStoredProc1.ParamByName('P').AsInteger := 1;
  OraStoredProc1.ExecSQL();
  str := OraStoredProc1.ParamByName('RESULT').AsString; // Works str = ''
  str := OraStoredProc1.ParamByName('RESULT').Value;    // Throw EVariantTypeCastError "Could not convert variant of type (Null) into type (OleStr)"

  OraStoredProc1.ParamByName('P').AsInteger := 0;
  OraStoredProc1.ExecSQL();
  str := OraStoredProc1.ParamByName('RESULT').AsString; // Works str = 'TEST'
  str := OraStoredProc1.ParamByName('RESULT').Value;    // Works str = 'TEST'
end;

Can you help?

Thanks
Ingo

MaximG
Devart Team
Posts: 1822
Joined: Mon 06 Jul 2015 11:34

Re: Exception when assigning NULL

Post by MaximG » Tue 06 Oct 2020 15:16

Thank you for the information. We have reproduced the issue and will investigate its origin. We will inform you about the results shortly.

MaximG
Devart Team
Posts: 1822
Joined: Mon 06 Jul 2015 11:34

Re: Exception when assigning NULL

Post by MaximG » Wed 07 Oct 2020 10:46

Previous versions of our components had an error: NULL values returned by the server were treated as Output parameters, which were then converted to an empty string.
The issue has been fixed in the latest version of ODAC. To get the necessary value, you can do the following:

Code: Select all

str := VarToStr(OraStoredProc1.ParamByName('RESULT').Value);

Post Reply