App hangs by calling same Form 5 times. First 4 times work very nice, fifth time app hangs

  

I just started developing a mobile app which has two forms, FRM_Main (Main Form) and FRM_Party (Party Form). We can open FRM_Party from FRM_Main by clicking on an Image. Code on Image is:
procedure TFRM_Main.IMAGE_PartyClick(Sender: TObject);
begin
FRM_Party := TFRM_Party.Create(Application);
FRM_Party.Show;
end;

Now, when FRM_Party calls the OnActivate event, I am loading some data in a TMSFMXTableView. That code is:
procedure TFRM_Party.FormActivate(Sender: TObject);
var
TableView : TTMSFMXTableViewItem;
I : Integer;
begin
if Class_My_Pro_and_func.Func_DataBaseConnection then // Checks wethere database connection is active or not if not then it connect with database and returns bool value.
Begin
UniQuery.Close;
UniQuery.SQL.Clear;
UniQuery.SQL.Text := ‘select * from db_stock.tbl_party where Reg_ID = :Reg_ID and Party_Delete <> :Party_Delete order by Party_Name ‘;
UniQuery.ParamByName(‘Reg_ID’).AsInteger := d_Glob_Reg_ID;
UniQuery.ParamByName(‘Party_Delete’).AsString := ‘F’;
UniQuery.Open;

TABLEVIEW_Party.BeginUpdate;

if UniQuery.RecordCount > 0 then
begin
for I := 1 to UniQuery.RecordCount do
begin
TableView := TABLEVIEW_Party.Items.Add;
TableView.Caption := UniQuery.Fields[1].AsString;
TableView.Description := UniQuery.Fields[2].AsString + ‘ = ‘ + UniQuery.Fields[3].AsString;

TABLEVIEW_Party.EndUpdate;

UniQuery.Next;
end;
end
else
ShowMessage(‘No recored Found.’);
End
end;

On FRM_Party is a Back button which takes the user to FRM_Main. This button code is:
procedure TFRM_Party.BTN_Party_BackClick(Sender: TObject);
begin
try
UniQuery.Connection.Close; // Closing Query connection
DB_Connection.Disconnect; // Disconnecting database
FreeAndNil(FRM_Party);
Close;
except
on E: Exception do
ShowMessage(E.Message);
end;
end;

Apart from the above code, nothing else is written on FRM_Party.
Now, the thing is that when I am running the application on mobile and opening/closing FRM_Party again and again, the first 4 times FRM_Party works very nice, but on the fifth time FRM_Party opens properly, but when closed the app hangs. I tried so many times by closing and opening the app. Every time the result is the same.
Can anyone help me?

Comments are closed.