Subject Re: [IBO] TIBOQuery posting error
Author Helen Borrie
At 10:58 PM 5/07/2005 +0200, you wrote:
>I'm using the code appearing below to update a FireBird table via an
>IntraWeb application:
>
> if UserSession.IBOPhotoAlbum.State = dsInsert then
> begin
> with UserSession.IBOPhotoAlbum do
> begin
> Close;
> SQL.Clear;
> SQL.Add('Insert into PhotoAlbum (Description, LoadedPhoto)');
> SQL.Add('Values (:Description, :LoadedPhoto)');
> ParamByName('Description').AsString := edtDescription.text;
> ParamByName('LoadedPhoto').LoadFromStream (UserSession.ms,
> ftGraphic);
> Open;
> end;
> end;
>
>But on executing the Open statement, I get the following error message
>although the record is loaded.
>
>class EIB_Error with message 'FieldNo: -1 not found'.
>
>The table has a unique key named ID. I increment this key via the
>following trigger code:
>
>AS
>BEGIN
> IF (NEW.ID is null) THEN
> NEW.ID = GEN_ID(photoalbum_id_gen, 1);
>END
>
>Apart from the error message, the updating seems fine.

Don't call Open and Close on an executable query - call ExecSQL.. Open and
Close are for datasets (SELECT statements).

More tips:

Use a TIB_DSQL for this statement. It carries none of the superfluous
overhead that you are getting in the iboquery. You can hook it up to your
TIBODatabase, using its IB_Connection property.

Don't clear and reassign the SQL if the operation is going to be executing
the same statement every time. Assign the SQL statically in the IDE, or
just once the first time you execute the statement. All you need to do
each time then is just assign the values to the params.

For an executable DML statement like this, it won't do any harm to test
that the statement is prepared, before you assign values to the params:

with YourStatementObject do
begin
...
if not Prepared then Prepare;
ParamByName('Description').AsString := edtDescription.text;
ParamByName('LoadedPhoto').Assign (YourImageStream);
Execute;
end;

Notice also the use of Assign to move the streamed image into the blob
parameter. There are some effective ways to work with blob in the native
IBO row type - look at TIB_ColumnBlob.

Helen