Dear, I have a problem when you want to update with SqlServer. I am developing a program with a DataSnap server and I have this problem when I want to update a field in the DBGrid.
I know that the error is in the key field of the sql query or rather, it is the key field that belongs to the database (field: code).
I do not know how to develop the sql query with delphi and that it does not generate that problem with my key code field.
State 01000, SQL Error Code 3621
State 23000, SQL Error Code 2627
Server-side code:
function TTServerMethods.updateItems(cod: String; desc: string; const pre:Integer): String;
begin
try
if not EjemploDatasnap.Connected then
EjemploDatasnap.Connected := true;
if SQLDataSetUpdateItems.Active then
SQLDataSetUpdateItems.Active := False;
SQLDataSetUpdateItems.ParamByName('codigops').AsString := cod;
SQLDataSetUpdateItems.ParamByName('descripcion').AsString := desc;
SQLDataSetUpdateItems.ParamByName('precio').AsInteger := pre;
SQLDataSetUpdateItems.ExecSQL;
SQLDataSetUpdateItems.Active := True;
//SQLDataSetIProductos.Active:=False;
except on
E: Exception do
Result := E.Message;
end;
end;
SQLDataSet code:
object SQLDataSetUpdateItems: TSQLDataSet
SchemaName = 'JDario'
CommandText =
'UPDATE productos SET Descripcion = :descripcion, Precio = :preci' +
'o WHERE codigo = :codigops'
MaxBlobSize = -1
Params = <
item
DataType = ftString
Name = 'descripcion'
ParamType = ptInput
end
item
DataType = ftInteger
Name = 'precio'
ParamType = ptInput
end
item
DataType = ftString
Name = 'codigops'
ParamType = ptInput
end>
SQLConnection = EjemploDataSnap
Left = 40
Top = 280
end
Client side code:
procedure TForm5.aceptarEdicionClick(Sender: TObject);
var client : TTServerMethodsClient;
var ds:TDataset;
var codigo : STring;
var Descripcion : STring;
var Precio : Integer;
begin
client := TTServerMethodsClient.Create(SQLConProductosGrid.DBXConnection);
begin
ds := DBGrid1.DataSource.DataSet;
if not SQLConProductosGrid.Connected then
SQLConProductosGrid.Connected := True;
DBGrid1.SetFocus;
codigo := ds.FieldByName('codigo').AsString;
Descripcion := ds.FieldByName('Descripcion').AsString;
Precio := ds.FieldByName('Precio').AsInteger;
Label3.Caption := client.updateItems(codigo,Descripcion,Precio);
ds.UpdateRecord;
ds.Next;
end;