Recent

Author Topic: with object do begin problems  (Read 3965 times)

douglas.cast

  • Jr. Member
  • **
  • Posts: 62
with object do begin problems
« on: June 04, 2016, 09:18:34 pm »
Before some research on programming over the web I've founded the with statement to work with connections inside of Lazarus (Zeos in my case), but by some reason I'm getting a SigSev error with the end; position (believe it or not).

Inside of my main form the I have this code and everything runs fine

Code: Pascal  [Select]
  1. try
  2.       btnOK.Enabled:=False;
  3.         with datamodule.qryRegister do begin
  4.           datamodule.connection.AutoCommit:=True;
  5.           datamodule.connection.StartTransaction;
  6.           SQL.Clear;
  7.           SQL.Text:=
  8.           'INSERT INTO table VALUES (:name, :sex, :dateofbirth, :maritalstatus)';
  9.           ParamByName('name').AsString:=teName.Text;
  10.           ParamByName('sex').AsInteger:=cbSex.ItemIndex;
  11.           ParamByName('dateofbirth').AsDate:=StrToDate(teDateOfBirth.Text);
  12.           ParamByName('maritalstatus').AsInteger:=cbMaritalStatus.ItemIndex;          
  13.           SQL.Clear;
  14.           SQL.Text:='SELECT LAST_INSERT_ID() AS iddb FROM table';
  15.           Open;
  16.           idUser:=FieldByName('iddb').AsInteger;
  17.           ExecSQL;
  18.           Close;        
  19.       try
  20.         datamodule.connection.Commit;
  21.         clearFields;
  22.       except
  23.         datamodule.connection.Rollback;
  24.         ShowMessage('Error');
  25.       end;
  26.     end; //with ends statement
  27.     except
  28.       ShowMessage('Error: no connection to the database');
  29.     end;
  30. bntOK.Enabled:=True;
  31.  

Inside of another form (availabe) and called with:

Code: Pascal  [Select]
  1. formPlaces:=tformPlaces.Create(nil);
  2. try
  3. formPlaces.ShowModal
  4. Finnaly
  5. formPlaces.Free;
  6.  

I have almost the same insert code that I have in main form.

Code: Pascal  [Select]
  1.   try
  2.           with datamodule.qryPlaces do begin
  3.             datamodule.connectionP.AutoCommit:=True;
  4.             datamodule.connectionP.StartTransaction;
  5.             SQL.Clear;
  6.             SQL.Text:=
  7.             'INSERT INTO places VALUES (:name, :owner, :phone';
  8.             ParamByName('name').AsString:=teName.Text;
  9.             ParamByName('owner').AsString:=teOwner.Text;
  10.             ParamByName('phone').AsString:=tePhone.Text;
  11.             ExecSQL;
  12.             Close;
  13.           end; // with end, if I try to put the this in the same place that I have in the first code I got the External SigSev error.
  14.                   // if its hier, thing works and all data is inserted into the database.              
  15.           try
  16.             icdm.icPlaces.Commit;
  17.             ClearFields;
  18.           except
  19.             icdm.icPlaces.Rollback;
  20.             ShowMessage('Error');
  21.           end;
  22.        // that end should be hier (at least I suppose.
  23.         except
  24.           ShowMessage('Error: no connection to the database');
  25.         end;
  26.  

Before some research I've founded someone saying that SigSev errors are usually caused when we try to call a non-existing object (deleted or so) but its no the case.

I think it makes more sense if I have the commit try inside of the with statement.

Any understaning about this is wellcome.

lainz

  • Hero Member
  • *****
  • Posts: 2912
    • Home
Re: with object do begin problems
« Reply #1 on: June 04, 2016, 09:37:52 pm »
And how is the code withouth using "with"

Blaazen

  • Hero Member
  • *****
  • Posts: 2676
  • POKE 54296,15
    • Eye-Candy Controls
Re: with object do begin problems
« Reply #2 on: June 04, 2016, 09:56:17 pm »
You cannot just add or delete end; this way, every end; must make sense in pascal code. The end; is end of with..do block, try..finally or try..except construction in this case.
Example (your second code): if you finish the with..do block earlier, where does ClearFields; belong to then?
Lazarus 2.1.0 r59757M FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.11.2, Plasma 5.14.2
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

douglas.cast

  • Jr. Member
  • **
  • Posts: 62
Re: with object do begin problems
« Reply #3 on: June 04, 2016, 11:28:40 pm »
You cannot just add or delete end; this way, every end; must make sense in pascal code. The end; is end of with..do block, try..finally or try..except construction in this case.
Example (your second code): if you finish the with..do block earlier, where does ClearFields; belong to then?

I don't get it, the 'make sense in pascal code' because the code is prety much the same, same situation only with a different number of SQL queryes, why Pascal can find sense in this code used in the main form and return a error with the same code in another form:

Code: Pascal  [Select]
  1. try
  2.  with do begin
  3.   try
  4.   except
  5.   end;
  6.  end; //with end
  7. except
  8. end;
  9.  

why in the second form this code works this way and not like above?

Code: Pascal  [Select]
  1. try
  2.  with do begin
  3.  end; //with end
  4.   try
  5.   except
  6.   end;
  7. except
  8. end;
  9.  

What exactly is making pascal 'lost' the sense is what I'm trying to understand, I've tryied to compare both codes side by side but I couldn't find what's wrong.

And how is the code withouth using "with"

The main form runs fine (second I've start with the with statement), but I'm using it to avoid a lot of

Code: Pascal  [Select]
  1.  datamodule.qry.comands
  2.  

kapibara

  • Sr. Member
  • ****
  • Posts: 493
Re: with object do begin problems
« Reply #4 on: June 05, 2016, 12:18:27 am »
All of it works. :-)

Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   try
  4.     with Button1 do
  5.     begin
  6.       try
  7.         ;
  8.       except
  9.         ;
  10.       end;
  11.     end;
  12.   except
  13.     ;
  14.   end;
  15. end;
  16.  

Code: Pascal  [Select]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. begin
  3.   try
  4.     with Button1 do
  5.     begin
  6.       ;
  7.     end; //with end
  8.     try
  9.     except
  10.       ;
  11.     end;
  12.   except
  13.     ;
  14.   end;
  15. end;
  16.  

Example 3 is how I do it, with try/end instead of begin/end:

Code: Pascal  [Select]
  1. procedure TForm1.Button3Click(Sender: TObject);
  2. begin
  3.   try
  4.     with Button1 do
  5.     try
  6.       ;
  7.     except
  8.       ;
  9.     end;
  10.   except
  11.     ;
  12.   end;
  13. end;
  14.  
Lazarus trunk / fpc 3.0.4 / Debian Stretch 64-bit

Ondrej Pokorny

  • Full Member
  • ***
  • Posts: 205
Re: with object do begin problems
« Reply #5 on: June 05, 2016, 02:28:42 pm »
Good advice: forget that with exists.

kapibara

  • Sr. Member
  • ****
  • Posts: 493
Re: with object do begin problems
« Reply #6 on: June 05, 2016, 06:39:48 pm »
I like "with" and use it for example to avoid declaring unnecessary variables:

Code: Pascal  [Select]
  1. with TForm2.Create(nil) do
  2. try
  3.   ShowModal;
  4. finally
  5.   Free;
  6. end;
Lazarus trunk / fpc 3.0.4 / Debian Stretch 64-bit

ezlage

  • Sr. Member
  • ****
  • Posts: 281
  • (Silêncio)
    • Lageteck
Re: with object do begin problems
« Reply #7 on: June 06, 2016, 01:17:30 am »
Try this way:

Code: Pascal  [Select]
  1. try
  2.       btnOK.Enabled:=False;
  3.         with datamodule.qryRegister do begin
  4.           datamodule.connection.AutoCommit:=False; //I CHANGED HERE
  5.           datamodule.connection.StartTransaction; //IMHO: YOU NEED THIS ONLY IF AutoCommit=False. TO USE IT, YOU NEED connection.Commit TOO, AND IT DEMANDS AutoCommit=False
  6.           SQL.Clear;
  7.           SQL.Text:=
  8.           'INSERT INTO table VALUES (:name, :sex, :dateofbirth, :maritalstatus);';
  9.           Params.ParseSQL(SQL.Text,True); //THIS MAKE SURE OF PARAMS PROBING
  10.           ParamByName('name').AsString:=teName.Text;
  11.           ParamByName('sex').AsInteger:=cbSex.ItemIndex;
  12.           ParamByName('dateofbirth').AsDate:=StrToDate(teDateOfBirth.Text);
  13.           ParamByName('maritalstatus').AsInteger:=cbMaritalStatus.ItemIndex;          
  14.           ExecSQL; //ExecSQL SHOULD BE HERE
  15.           SQL.Clear;
  16.           SQL.Text:='SELECT LAST_INSERT_ID() AS iddb FROM table;';
  17.           Open;
  18.           idUser:=FieldByName('iddb').AsInteger;
  19.           Close;        
  20.       try
  21.         datamodule.connection.Commit; //ARE NOT NECESSARY WHEN AutoCommit=True
  22.         clearFields;
  23.       except
  24.         datamodule.connection.Rollback;
  25.         ShowMessage('Error');
  26.       end;
  27.     end; //with ends statement
  28.     except
  29.       ShowMessage('Error: no connection to the database');
  30.     end;
  31. bntOK.Enabled:=True;
  32.  
« Last Edit: June 06, 2016, 01:27:41 am by ezlage »
Sorry by my poor english.

Atenciosamente,

Ezequiel Lage
ezequiel@lageteck.com

https://github.com/ezlage, https://github.com/ezlage/sempercom

ezlage

  • Sr. Member
  • ****
  • Posts: 281
  • (Silêncio)
    • Lageteck
Re: with object do begin problems
« Reply #8 on: June 06, 2016, 01:24:08 am »
Do you really need a "try" inside other "try"?
IMHO, if an exception raises, maybe only the outer "except" block will be called.
Sorry by my poor english.

Atenciosamente,

Ezequiel Lage
ezequiel@lageteck.com

https://github.com/ezlage, https://github.com/ezlage/sempercom

ezlage

  • Sr. Member
  • ****
  • Posts: 281
  • (Silêncio)
    • Lageteck
Re: with object do begin problems
« Reply #9 on: June 06, 2016, 01:30:27 am »
Code: Pascal  [Select]
  1. try
  2.           with datamodule.qryPlaces do begin
  3.             datamodule.connectionP.AutoCommit:=False; //CHANGED HERE
  4.             datamodule.connectionP.StartTransaction;
  5.             SQL.Clear;
  6.             SQL.Text:='INSERT INTO places VALUES (:name, :owner, :phone);'; //CHANGED HERE
  7.             Params.ParseSQL(SQL.Text,True); //THIS MAKE SURE OF PARAMS PROBING
  8.             ParamByName('name').AsString:=teName.Text;
  9.             ParamByName('owner').AsString:=teOwner.Text;
  10.             ParamByName('phone').AsString:=tePhone.Text;
  11.             ExecSQL;
  12.             Close; //NO NEED
  13.           end; // with end, if I try to put the this in the same place that I have in the first code I got the External SigSev error.
  14.                   // if its hier, thing works and all data is inserted into the database.              
  15.           try
  16.             icdm.icPlaces.Commit;
  17.             ClearFields;
  18.           except
  19.             icdm.icPlaces.Rollback;
  20.             ShowMessage('Error');
  21.           end;
  22.        // that end should be hier (at least I suppose.
  23.         except
  24.           ShowMessage('Error: no connection to the database');
  25.         end;
  26.  
Sorry by my poor english.

Atenciosamente,

Ezequiel Lage
ezequiel@lageteck.com

https://github.com/ezlage, https://github.com/ezlage/sempercom

douglas.cast

  • Jr. Member
  • **
  • Posts: 62
Re: with object do begin problems
« Reply #10 on: July 08, 2016, 08:01:16 pm »
Before some long research in my code and a lot of splitting, I've found the problem, I've created a 'clearFields' procedure to clear some tfield on the form, but when I typed the 'ClearFields' (capital C) using the CTRL+Spacebar in the application I've switched to a SQL function that clear the fields, so like someone said my end was in the wrong place, because the SQL function works in a different way, after noting that and switch the procedure name to clearFormFields everything is working right.

Thanks the replys and the patience, sorry my english.