Recent

Author Topic: [SOLVED] Struggling with multiple forms, DBGrids and SQLite  (Read 1940 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
[SOLVED] Struggling with multiple forms, DBGrids and SQLite
« on: August 22, 2020, 08:55:28 pm »
I need some help! I'm going insane.

I have
3 x TSQLQuery
3 x TDataSource
1 x SQLite3Connection
1 x SQLTranscaction

The 3 TDataSources are each 'connected' to the 3 respective SQLQueries. And each SQLQuery is connected to the SQLite3Connection.

Then, I have 3 forms, and in each form, is a DBGrid. Each DBGrid of each form is linked to the corresponding DataSource.

Of the 3, 2 work fine. They are made visible and shows columns and rows of data. However, the third one, which I thought I made in exactly the same way just will not show any data. The form is empty when made visible. And if I add a line like the following, I get "Project X raised exception class 'External : SEGSEGV in file x.pas at line X : frmDisplayGrid3.DBGrid3.Options := frmDisplayGrid3.DBGrid3.Options + [dgAutoSizeColumns]; :

Code: Pascal  [Select][+][-]
  1.     frmDisplayGrid3.DBGrid3.Options := frmDisplayGrid3.DBGrid3.Options + [dgAutoSizeColumns];
  2.  

I have spent literally hours trying to see where I might not have a uses clause added or something. And I just can't understand why it won't display the content. It seems as though the form can't hold the data or something?

I have even tried quering the SQLite database by adding a breakpoint to the code just before I try to make the 3rd form visible. It definitely exists and it holds all the data I want to show in the 3rd form. But I just can't get it to show or be accepted into the form.

I can't easily paste the code relating to it because it spans several units and forms but if needs be I'll try. I'm hoping for now someone might be able to identify what the issue is most likely to be given the above error description. i.e. empty form with the line above commented out, and with it in, I get "Project X raised exception class 'External : SEGSEGV"

« Last Edit: August 24, 2020, 05:26:17 pm by Gizmo »

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Struggling with multiple forms, DBGrids and SQLite
« Reply #1 on: August 23, 2020, 01:40:04 pm »
Can you make a working example?
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

egsuh

  • Hero Member
  • *****
  • Posts: 1296
Re: Struggling with multiple forms, DBGrids and SQLite
« Reply #2 on: August 24, 2020, 07:38:58 am »

Aren't you creating the form after the following statement?

  frmDisplayGrid3.DBGrid3.Options := frmDisplayGrid3.DBGrid3.Options + [dgAutoSizeColumns];

Check the creation of components, especially if you are using frames.


kapibara

  • Hero Member
  • *****
  • Posts: 610
Re: Struggling with multiple forms, DBGrids and SQLite
« Reply #3 on: August 24, 2020, 10:19:42 am »
I think this happened for me too. The reason was that something was being used that was not ready/opened/created yet. It was a config simple mistake, but hard on the sanity until found out.

To start with, set DBGrid3.DataSource to "None" in the object inspector and see if it works now.

Code: Pascal  [Select][+][-]
  1. frmDisplayGrid3.DBGrid3.Options := frmDisplayGrid3.DBGrid3.Options + [dgAutoSizeColumns];
     
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Struggling with multiple forms, DBGrids and SQLite
« Reply #4 on: August 24, 2020, 11:12:02 am »
Code: Pascal  [Select][+][-]
  1.     frmDisplayGrid3.DBGrid3.Options := frmDisplayGrid3.DBGrid3.Options + [dgAutoSizeColumns];
  2.  
Does this code belong to a method of class TfrmDisplayGrid3? Then remove the "frmDisplayGrid3" from this and any other methods of TfrmDisplayGrid3. Because having the instance name in the class code means that the code will work only when the instance is named exactly like that. If you do not auto-create the form there is a high chance that this code will fail because there is no "frmDisplayGrid3".

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Struggling with multiple forms, DBGrids and SQLite
« Reply #5 on: August 24, 2020, 12:41:59 pm »
Thanks guys for the suggestions.

Re the form creation aspect. The LPR project file creates all the forms at startup as far I am aware with the following lines :

Code: Pascal  [Select][+][-]
  1. ...
  2. Application.CreateForm(TMainForm, MainForm);
  3. Application.CreateForm(TfrmDisplayGrid1, frmDisplayGrid1);
  4. Application.CreateForm(TfrmDisplayGrid3, frmDisplayGrid3);
  5. Application.CreateForm(TfrmAbout, frmAbout);
  6. Application.CreateForm(TfrmSQLiteDBases, frmSQLiteDBases);  
  7. ...
  8. Application.Run;
  9.  

So all the forms should be created by the time they are called.

On the class point raised by WP...I did try changing the line from

Code: Pascal  [Select][+][-]
  1.     frmDisplayGrid3.DBGrid3.Options := frmDisplayGrid3.DBGrid3.Options + [dgAutoSizeColumns];
  2.  

to

Code: Pascal  [Select][+][-]
  1.     DBGrid3.Options := DBGrid3.Options + [dgAutoSizeColumns];
  2.  

but then when compiling it said the identifier could not be found and was unknown.

Re the DataSource for DBGrid3 to none suggestion...for that particular component (the DBGrid3) the datasource is already blank (none). It is the SQLQueries that have the datasources attached to them. The other two DBGrids that work get populated via the SQLQueries but the same technique for this one just wont work.

So I have resorted to deleting it, and trying again with a new form and a new DBGrid. Added all the usual uses to link it together. I noticed this time that in the object inspector for the new DBGrid, in the "Datasource" drop down, all three of the datasource options are now listed, whereas they were not before for this 3rd grid (yet for the other two, they are listed). So I figured the problem must have been some kind of invisibility of the datasources by the form. (It's worth noting that the two that do work do not actually have either datasource selected; they are listed as none. But they are available). So I selected DataSource3 just to see if that would work, assuming that would now resolve it given they were inaccessible before, but sadly on execution exactly the same problem occured.

And if I try using

frmDisplayGrid3.DBGrid3.DataSource.DataSet.Open;

that line errors during running suggesting the dataset can't be opened or is invalid. So somewhere I think the DataSource is not getting linked or is inaccessible, but I keep looking at the other two and comparing it against the setting for DataSource3 and all the settings are exactly the same, just changed by name or by SQLQuery link.

This is driving me utterly insane. I can't understand how I can have a main form, with 2 additional forms all containing a DBGrid, and have corresponding TSQLQueries and Datasources, all created at application launch in the same way, and yet this third form and its embedded DBGrid seems somehow inaccessible. I just cannot see what I have done wrong.
« Last Edit: August 24, 2020, 12:46:53 pm by Gizmo »

egsuh

  • Hero Member
  • *****
  • Posts: 1296
Re: Struggling with multiple forms, DBGrids and SQLite
« Reply #6 on: August 24, 2020, 02:10:13 pm »
Your explanations are a little bit confusing.

Quote
for that particular component (the DBGrid3) the datasource is already blank (none). It is the SQLQueries that have the datasources attached to them.

Datasource property is necessary for DB controls, e.g. DBGrid in this case. It must be set as  DBGrid3.datasource = datasource3, and datasource3.DataSet = SQLQuery3.


Code: Pascal  [Select][+][-]
  1.  frmDisplayGrid3.DBGrid3.Options := frmDisplayGrid3.DBGrid3.Options + [dgAutoSizeColumns];

If you have defined this statement within OnCreate event handler of TfrmDisplayGrid3, this should raise exception because frmDisplayGrid3 is not instantiated yet. Or you are running the above sentece within the TMainForm, it will also raise SEGSEGV error.

Code: Pascal  [Select][+][-]
  1. DBGrid3.Options := DBGrid3.Options + [dgAutoSizeColumns];


"Identifier not found here" means DBGrid3 may be defined in other unit, and the unit is not included in the uses clause. 

You have four forms, so please explain which form contains which components.
« Last Edit: August 24, 2020, 02:11:53 pm by egsuh »

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Struggling with multiple forms, DBGrids and SQLite
« Reply #7 on: August 24, 2020, 02:16:51 pm »
Without seeing your code it is not possible to solve this.

Make a copy of the project folder. Then remove everything from the copied project which is not needed to reproduce the error. Test whether the modified project still compiles and shows the error. Then pack the .pas, .lfm, .lpi, lpr and sqlite3 files into a common zip which you can upload here.

If you do not want to show the project in public upload it to some cloud server and send me the link by PM. I'll try to have a look and see what's wrong. But please: Give detailed instructions what to click to trigger the error - I am not willing to learn how to use a foreign program.

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Struggling with multiple forms, DBGrids and SQLite
« Reply #8 on: August 24, 2020, 04:27:21 pm »
After seeing the code sent by a PM, I saw that the {$R *.lfm} was missing from the unit containing frmDisplayGrid3. As a consequence the DBGrid on this form was nil at runtime.

Didn't know about the importance of these few characters...

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: [SOLVED] Struggling with multiple forms, DBGrids and SQLite
« Reply #9 on: August 24, 2020, 05:28:30 pm »
WP is the man, the legend, and my hero of the hour!!

As he reports, I sent him the project and he quickly identified the missing line below "implementation". I too had never appreciated the importance of this line! So I now have

Code: Pascal  [Select][+][-]
  1. var
  2.   frmDisplayGrid3: TfrmDisplayGrid3;
  3.  
  4. implementation
  5.  
  6. {$R *.lfm}  // this was added by WP
  7.  
  8. initialization
  9.                        
  10.  

and it works!! All the hours spent struggling with that....I'm an idiot.

Thank you so much WP. I really do appreciate your generous contribution of time and effort.


 

TinyPortal © 2005-2018