PDA

Ver la Versión Completa : error en código?


identsoft
23-11-2007, 09:53:58
Este código no funciona correctamente

procedure TfrmBancos.Button1Click(Sender: TObject);
begin
SQLDataSet1.Close;
ClientDataSet1.Close;
SQLDataSet1.CommandText:= ' ';
if (Length( edit1.Text) > 0) then
begin
SQLDataset1.CommandText := 'SELECT * FROM BANCOS WHERE NOM_BANCO LIKE :P_NBANCO';
SQLDataSet1.ParamByName('P_NBANCO').AsString := Edit1.Text + '%';
end
else
begin
SQLDataSet1.CommandText := 'SELECT * FROM BANCOS ORDER BY BANCO' ;
end;
Edit1.Clear;
SQLDataSet1.Open;
ClientDataSet1.Open;
end;

La primera vez lo hace correctamente. La segunda, si No introduces un texto en el edit1 (deberia hacer el else), arroja un error de que no existe el campo :P_NBANCO (que no es un campo de la bd sino un parámetro). Si introduces un texto, se queda como está, no sale el error pero no aparece nada.

y este otro sí

procedure TfrmBancos.btnBancoClick(Sender: TObject);
begin
SQLDataSet1.Close;
ClientDataSet1.Close;
SQLDataSet1.CommandText := '';
if Length(edtBanco.Text)> 0 then
begin
edtBanco.Text := edtBanco.Text + '%';
SQLDataSet1.CommandText := 'select * from bancos where nom_banco like ' +
QuotedStr(edtBanco.Text);
end
else
begin
SQLDataSet1.CommandText := ' select * from bancos order by banco';
end;
edtBanco.Clear;
SQLDataSet1.Open;
ClientDataSet1.Open;
end;

Los datos los saco a una grid.
Utilizo la BD es firebird 1.5 y Delphi 2006
No lo comprendo, ¿me podeis explicar que es lo que falta o qué estoy haciendo mal?

identsoft
23-11-2007, 09:55:33
Edit1 y edtBanco es lo mismo

basti
23-11-2007, 13:11:10
Supongo que tienes enlazado SQLDataset y ClientDataSet a través de un proveedor. Si es así, no debes abrir y cerrar el SQLDataSet, de eso ya se encarga el proveedor. Es posible que el error venga de ahí.

procedure TfrmBancos.Button1Click(Sender: TObject);
begin
ClientDataSet1.Close;
SQLDataSet1.CommandText:= ' ';
if (Length( edit1.Text) > 0) then
begin
SQLDataset1.CommandText := 'SELECT * FROM BANCOS WHERE NOM_BANCO LIKE :P_NBANCO';
SQLDataSet1.ParamByName('P_NBANCO').AsString := Edit1.Text + '%';
end
else
begin
SQLDataSet1.CommandText := 'SELECT * FROM BANCOS ORDER BY BANCO' ;
end;
Edit1.Clear;
ClientDataSet1.Open;
end;

identsoft
26-11-2007, 13:37:32
Gracias basti por responder, pero no es esa la solución.
En fin me quedaré con las ganas.
Un saludo (en espacial a basti)