cmdUpdateFoyer: begin // read the size of the stream sent by the client LStreamSize := StrToInt(LClientContext.Connection.IOHandler.ReadLn); // read the stream LClientContext.Connection.IOHandler.ReadStream(LStream, LStreamSize, False); // create the foyers collection LFoyerRecord := TFoyers.Create; // create the proxy object for managing the collection LProxy := TProxy.Create(nil); // convert the stream received from the client to a collection LProxy.ReadFromStream(LStream, LFoyerRecord); // try with zqryFoyers do begin // try // Open the dataset Open; // Set the SQL text to // insert a record if the ID parameter is zero, edit an existing record if it is not if StrToInt(LFoyerRecord[0].PropertyString[0]) = 0 then SQL.Text := SQLInsertFoyerRecord else SQL.Text := SQLModifyFoyerRecord; //ShowMessage(SQL.Text); // set the parameter values for intCount := 0 to zqryFoyers.Params.Count - 1 do if (intCount = 0) or (intCount = 3) then // 0 - ID primary key field; 3 - ville foreign key field zqryFoyers.Params[intCount].AsInteger := StrToInt(LFoyerRecord[0].PropertyString[intCount]) else zqryFoyers.Params[intCount].AsString := LFoyerRecord[0].PropertyString[intCount]; // Execute the SQL statement ExecSQL; // commit the changes to the table Connection.Commit; // set the operation results to 'success' strOperationFlag := strServerUpdateRecordSuccess; except // rollback the transaction because it failed! Connection.Rollback; // set the operation results to 'failure' strOperationFlag := strServerUpdateRecordFailure; end; // close the query Close; end;
zqryFoyers.Params[intCount].AsString := LFoyerRecord[0].PropertyString[intCount];
ExecSQL;
if (intCount = 0) or (intCount = 3) then // 0 - ID primary key field; 3 - ville foreign key field
What is the stack trace when the error occurs? Does it point to Code: [Select]zqryFoyers.Params[intCount].AsString := LFoyerRecord[0].PropertyString[intCount]; or Code: [Select]ExecSQL;
Also Code: [Select]if (intCount = 0) or (intCount = 3) then // 0 - ID primary key field; 3 - ville foreign key field doesn't seem to correspond with your insert query. If I count correctly, FK_ID_VILLE is index 2 instead of 3. It looks like your insert query is omitting the Foyer_id (or whatever is called) and your parameters are off by 1. Edit: When your parameters are one off, you are trying to assign the value of Adresse to FK_ID_VILLE which would explain the EConvertError
By the way, if I type the the following insert statement into a Firebird DB Admin software like IBExpert or FlameRobin and run it, it works without problems
insert into foyers (Nom, Adresse, Fk_ID_Ville, Telephone, Contact) values (0,'Sir Lancelot', 'At Home', 40, '9876543210')
See the attached picture for the zqryFoyers paramaters list.
When you assign a new sql to a zquery the parameter list is re-created from the query...
But does this mean that I no longer need to create parameters in the design mode since the INSERT/MODIFY statements will automatically recreate the parameter lists.
The DataType gets set implicitly by methods like TParam.AsString, etc.
Wouldn't the DataType property be set by FPC? Then using AsString etc casts the value between the DataType and the specified type...
sql.query.text:='update table set name=:name where ID=:ID';//sql.params[0].DataType is ftUnknownsql.params[0].AsString:='BigChimp';//sql.params[0].DataType is now ftString
So in fact setting params[n].DataType is rather superfluous as you can use the As... setters/getters anwyay?
q1.sql.Text:='insert into testdate values (:idx,:dt) returning dt into :dtr';q1.Params.ParamByName('idx').asinteger := random(100);q1.Params.ParamByName('dt').asdate := now();q1.Params.ParamByName('dtr').ParamType:=ptOutput;q1.Params.ParamByName('dtr').DataType:=ftDate;q1.ExecSQL;v:=q1.Params.ParamByName('dtr').AsDate;
q1.Params.ParamByName('guid').AsString='{21EC2020-3AEA-1069-A2DD-08002B30309D}';q1.Params.ParamByName('guid').DataType:=ftGuid;