Search code examples
sql-serverdelphidelphi-7ado

Passing BLOB as Parameter causes Error: Operand type clash: image is incompatible with text


I am working on Delphi 7+ SQL Server 2008 + ADO

I have a table with 4 fields

emp_id integer 
Name varchar(50) 
designation varchar(10) 
comment text

I am trying to insert data from Delphi

 with qryAdoEmployee do
        try
          if not prepared then Prepared := True;
          Parameters.ParamByName('emp_id').Value := 12345;
          Parameters.ParamByName('Name').Value := 'NAME';
          Parameters.ParamByName('designation').Value := 'NEWDesig' ;

so i tried below line of code to insert data into comment of type ftBlob

          parameters.ParamByName('comment').Assign(Memo1.Lines);

with above line of code it is inserting data into database but it is inserting wrong data like "???5?????????????????????????"

so after spending some time on google replaced above line of code with

      Len := Length(Memo1.Lines.Text);
      Stream := TMemoryStream.Create;
      Stream.Write(Len, SizeOf(Len));
      Stream.Write(PChar(Memo1.Lines.Text)^, Len);
      parameters.ParamByName('comment').LoadFromStream(Stream,ftBlob);

above piece of code is throwing error Operand type clash: image is incompatible with text

    ExecSQL;
    except on E:EDatabaseError do 
      MessageDlg(E.Message, mtError, [mbOK], 0);
    end;

Solution

  • SQL Server text type should map to ftMemo in Delphi (not to ftBlob).

    If you defined the parameters in design time, make sure the DataType is set to ftMemo, or set it in runtime:

    parameters.ParamByName('comment').DataType := ftMemo;
    

    Then simply assignn the parameter Value:

    parameters.ParamByName('comment').Value := Memo1.Text;