hidrau
asked on
How can I get the message error from my adoquery?
Hello Guys,
Is There any way to get the message error when I try to run my query and It gives me error with foreignkey; Please See my code:
Try
TQ2.Close;
TQ2.Sql.Clear;
TQ2.Sql.Add(TQ.FieldByName ('LN_LINEC OMAND').As String);
TQ2.ExecSQL;
Except
Get error in string variable and show to user with showmessage
End;
Thanks
Is There any way to get the message error when I try to run my query and It gives me error with foreignkey; Please See my code:
Try
TQ2.Close;
TQ2.Sql.Clear;
TQ2.Sql.Add(TQ.FieldByName
TQ2.ExecSQL;
Except
Get error in string variable and show to user with showmessage
End;
Thanks
so in your case:
try
TQ2.Close;
TQ2.Sql.Clear;
TQ2.Sql.Add(TQ.FieldByName ('LN_LINEC OMAND').As String);
TQ2.ExecSQL;
except
on E: Exception do
ShowMessage(Format('%s - %s', [E.ClassName, E.Message]));
end;
ziolko.
try
TQ2.Close;
TQ2.Sql.Clear;
TQ2.Sql.Add(TQ.FieldByName
TQ2.ExecSQL;
except
on E: Exception do
ShowMessage(Format('%s - %s', [E.ClassName, E.Message]));
end;
ziolko.
I would also recommend to include
ShowMessage(TQ.FieldByName ('LN_LINEC OMAND').As String);
it will be handy for debugging.
ShowMessage(TQ.FieldByName
it will be handy for debugging.
ASKER
Why I am having error after this code:
on E: Exception do
ShowMessage(E.ClassName + ' ' + E.Message);
it is giving me the message: END, expected but identifier "MudaTentativaErroGravacao " found
"MudaTentativaErroGravacao " is a function created by me
TQ2.ExecSQL;
Except
on E: Exception do
ShowMessage(E.ClassName + ' ' + E.Message);
MudaTentativaErroGravacao( ID);
GravaErroLog('Erro ao grava linha de comando ID:' + ID + ' na Conexão: ' + CnxBS);
End;
on E: Exception do
ShowMessage(E.ClassName + ' ' + E.Message);
it is giving me the message: END, expected but identifier "MudaTentativaErroGravacao
"MudaTentativaErroGravacao
TQ2.ExecSQL;
Except
on E: Exception do
ShowMessage(E.ClassName + ' ' + E.Message);
MudaTentativaErroGravacao(
GravaErroLog('Erro ao grava linha de comando ID:' + ID + ' na Conexão: ' + CnxBS);
End;
try this:
TQ2.ExecSQL;
Except
on E: Exception do begin
ShowMessage(E.ClassName + ' ' + E.Message);
MudaTentativaErroGravacao( ID);
GravaErroLog('Erro ao grava linha de comando ID:' + ID + ' na Conexão: ' + CnxBS);
endl
End;
TQ2.ExecSQL;
Except
on E: Exception do begin
ShowMessage(E.ClassName + ' ' + E.Message);
MudaTentativaErroGravacao(
GravaErroLog('Erro ao grava linha de comando ID:' + ID + ' na Conexão: ' + CnxBS);
endl
End;
ooops typo, should be:
TQ2.ExecSQL;
Except
on E: Exception do begin
ShowMessage(E.ClassName + ' ' + E.Message);
MudaTentativaErroGravacao( ID);
GravaErroLog('Erro ao grava linha de comando ID:' + ID + ' na Conexão: ' + CnxBS);
end;
End;
@FactorB to be really >>handy for debugging.<<
I would add
ShowMessage(TQ2.SQL.Text);
not only field value
ziolko
TQ2.ExecSQL;
Except
on E: Exception do begin
ShowMessage(E.ClassName + ' ' + E.Message);
MudaTentativaErroGravacao(
GravaErroLog('Erro ao grava linha de comando ID:' + ID + ' na Conexão: ' + CnxBS);
end;
End;
@FactorB to be really >>handy for debugging.<<
I would add
ShowMessage(TQ2.SQL.Text);
not only field value
ziolko
also you might use:
MessageDlg(E.ClassName + ' ' + E.Message, mtError, [mbOK], 0);
instead of
ShowMessage(E.ClassName + ' ' + E.Message)
ziolko.
MessageDlg(E.ClassName + ' ' + E.Message, mtError, [mbOK], 0);
instead of
ShowMessage(E.ClassName + ' ' + E.Message)
ziolko.
It will be better organized if you wrap the text
ShowMessage(E.ClassName+#1 0#13+E.Mes sage+#10#1 3+TQ2.SQL. Text+#10#1 3+TQ.Field ByName('LN _LINECOMAN D').AsStri ng);
Regards,
B
ShowMessage(E.ClassName+#1
Regards,
B
hmm just noticed :
TQ2.Sql.Add(TQ.FieldByName ('LN_LINEC OMAND').As String);
does that mean that LN_LINECOMAND contains SQL command text?
if so, you don't need to display TQ2.SQL.Text AND TQ.FieldByName('LN_LINECOM AND').AsSt ring
because it's the same string
ziolko.
TQ2.Sql.Add(TQ.FieldByName
does that mean that LN_LINECOMAND contains SQL command text?
if so, you don't need to display TQ2.SQL.Text AND TQ.FieldByName('LN_LINECOM
because it's the same string
ziolko.
ASKER
Hi Ziolko
Take a look at my code.
I am getting go on with it. After on E: Exception do GravaErroLog(E.ClassName + ' ' + E.Message);
the next code is getting that error
Take a look at my code.
I am getting go on with it. After on E: Exception do GravaErroLog(E.ClassName + ' ' + E.Message);
the next code is getting that error
Function TFserviceReplicador.RodaTabErro: Boolean;
Procedure MudaTentativaErroGravacao(ID: String);
Var
TQ: TADOQuery;
Begin
TQ := TADOQuery.Create(Nil);
TQ.Connection := cnx1;
Try
With TQ do Begin
Close;
Sql.Clear;
Sql.Add('UPDATE CTRGRVERRO set Ntentativas=Ntentativas+1');
Sql.Add('WHERE ID=' + ID);
ExecSQL;
End;
Finally
FreeAndNil(TQ)
End;
End;
Var
TQ, TQ2 : TADOQuery;
CnxBS, BS, IP, ID : String;
begin
TQ := TADOQuery.Create(Nil);
TQ.Connection := cnx1;
TQ2 := TADOQuery.Create(Nil);
Try
with Tq do Begin
Close;
Sql.Clear;
Sql.Add('SELECT A.*, B.LN_DATAHORA, B.LN_LINECOMAND FROM CTRGRVERRO A');
Sql.Add('INNER JOIN LINHACOMANDO B ON B.LN_ID=A.CODLINH');
Sql.Add('AND A.NTentativas < 4');
Sql.Add('ORDER BY A.CONEXAO, B.LN_ID');
Open;
End;
While Not TQ.Eof do Begin
CnxBS := TQ.FieldByName('CONEXAO').AsString;
BS := TQ.FieldByName('BASE').AsString;
IP := TQ.FieldByName('IPping').AsString;
ID := TQ.FieldByName('ID').AsString;
TQ2.ConnectionString := 'Provider=SQLNCLI.1;Password=cj2492;Persist Security Info=True;User ID=sa;Initial Catalog=' + BS + ';Data Source=' + CnxBS;
If HaLink(IP) Then Begin
Try
TQ2.Close;
TQ2.Sql.Clear;
TQ2.Sql.Add(TQ.FieldByName('LN_LINECOMAND').AsString);
TQ2.ExecSQL;
TQ2.Close;
TQ2.ConnectionString := cnx1.ConnectionString;
TQ2.Sql.Clear;
TQ2.Sql.Add('DELETE FROM CTRGRVERRO WHERE ID=' + TQ.FieldByName('ID').AsString);
TQ2.ExecSQL;
Except
on E: Exception do GravaErroLog(E.ClassName + ' ' + E.Message);
MudaTentativaErroGravacao(ID);
GravaErroLog('Erro ao grava linha de comando ID:' + ID + ' na Conexão: ' + CnxBS);
End;
End Else Begin
GravaErroLog('Sem link para roda linha de comando ID:' + TQ.FieldByName('ID').AsString + ' na Conexão: ' + CnxBS);
End;
TQ.Next;
End;
Finally
FreeAndNil(TQ);
FreeAndNil(TQ2)
End;
End;
first of all do you want:
MudaTentativaErroGravacao( ID);
GravaErroLog('Erro ao grava linha de comando ID:' + ID + ' na Conexão: ' + CnxBS);
to be executed ONLY when error occurs or everytime?
next thing you call TQ2.ExecSQL twice in same try/except block you must be aware that when first ExecSQL fails with exception then second ExecSQL will not be executed
Try
TQ2.Close;
TQ2.Sql.Clear;
TQ2.Sql.Add(TQ.FieldByName ('LN_LINEC OMAND').As String);
TQ2.ExecSQL; << if this will throw exception....
TQ2.Close;
TQ2.ConnectionString := cnx1.ConnectionString;
TQ2.Sql.Clear;
TQ2.Sql.Add('DELETE FROM CTRGRVERRO WHERE ID=' + TQ.FieldByName('ID').AsStr ing);
TQ2.ExecSQL; <<... this will NOT be executed
Except
also in this function:
Procedure MudaTentativaErroGravacao( ID: String);
Var
TQ: TADOQuery;
Begin
TQ := TADOQuery.Create(Nil);
TQ.Connection := cnx1;
Try
With TQ do Begin
Close;
Sql.Clear;
Sql.Add('UPDATE CTRGRVERRO set Ntentativas=Ntentativas+1' );
Sql.Add('WHERE ID=' + ID);
ExecSQL; << there's no try/except to catch errors raised by this
End;
Finally
FreeAndNil(TQ)
End;
End;
ziolko.
MudaTentativaErroGravacao(
GravaErroLog('Erro ao grava linha de comando ID:' + ID + ' na Conexão: ' + CnxBS);
to be executed ONLY when error occurs or everytime?
next thing you call TQ2.ExecSQL twice in same try/except block you must be aware that when first ExecSQL fails with exception then second ExecSQL will not be executed
Try
TQ2.Close;
TQ2.Sql.Clear;
TQ2.Sql.Add(TQ.FieldByName
TQ2.ExecSQL; << if this will throw exception....
TQ2.Close;
TQ2.ConnectionString := cnx1.ConnectionString;
TQ2.Sql.Clear;
TQ2.Sql.Add('DELETE FROM CTRGRVERRO WHERE ID=' + TQ.FieldByName('ID').AsStr
TQ2.ExecSQL; <<... this will NOT be executed
Except
also in this function:
Procedure MudaTentativaErroGravacao(
Var
TQ: TADOQuery;
Begin
TQ := TADOQuery.Create(Nil);
TQ.Connection := cnx1;
Try
With TQ do Begin
Close;
Sql.Clear;
Sql.Add('UPDATE CTRGRVERRO set Ntentativas=Ntentativas+1'
Sql.Add('WHERE ID=' + ID);
ExecSQL; << there's no try/except to catch errors raised by this
End;
Finally
FreeAndNil(TQ)
End;
End;
ziolko.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks very much
ASKER
ziolko:, when I got the error it run this line
Except
on E: Exception do GravaErroLog(E.ClassName + ' ' + E.Message);
End;
and after running it returns to this line again
Try
TQ2.Close;
TQ2.Sql.Clear;
TQ2.Sql.Add(TQ.FieldByName ('LN_LINEC OMAND').As String);
TQ2.ExecSQL;
why?
Except
on E: Exception do GravaErroLog(E.ClassName + ' ' + E.Message);
End;
and after running it returns to this line again
Try
TQ2.Close;
TQ2.Sql.Clear;
TQ2.Sql.Add(TQ.FieldByName
TQ2.ExecSQL;
why?
because it is in WHILE loop
if you want to break loop after first error use this:
Except
on E: Exception do begin
GravaErroLog(E.ClassName + ' ' + E.Message);
Break;
end;
End;
note that if you do so
this:
MudaTentativaErroGravacao( ID);
GravaErroLog('Erro ao grava linha de comando ID:' + ID + ' na Conexão: ' + CnxBS);
will NOT be executed
ziolko.
if you want to break loop after first error use this:
Except
on E: Exception do begin
GravaErroLog(E.ClassName + ' ' + E.Message);
Break;
end;
End;
note that if you do so
this:
MudaTentativaErroGravacao(
GravaErroLog('Erro ao grava linha de comando ID:' + ID + ' na Conexão: ' + CnxBS);
will NOT be executed
ziolko.
except
on E: Exception do
ShowMessage(E.ClassName + ' ' + E.Message);
end;
ziolko.