-3

After selecting the name of the table in my bd grid I want to have the colones that belongs to this table by double clicking but I find this error

lorque j double click Exception 'first chance' to $ 00840BD7 Exception class $ C0000005 with a message 'access violation at 0x00840bd7: read of address 0x00000000'. Process conversion.exe (5644) " here is the code :

procedure TForm5.DBGrid1DblClick(Sender: TObject);

begin
    FDQuery2.SQL.Clear;
FDQuery2.SQL.Text := 'select column_name from information_schema.columns where table_schema="bases" and table_name = "'+DBGrid2.SelectedField.ToString+'"';
FDQuery2.Open;
ListBox2.Clear;
FDQuery2.GetFieldNames(ListBox2.Items);
end;

can you help me ?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
sasa
  • 31
  • 7
  • 1
    Maybe you should specify on which line you get the access violation. Did you try to step through with the debugger and see which object reference is causing it? – Frazz Apr 03 '17 at 08:41
  • The error message is clear enough. You have a nil pointer or reference somewhere. Why aren't you debugging this yourself? Why ask here before doing basic debugging? Or is your question actually about what the error message means? – David Heffernan Apr 03 '17 at 08:48
  • What is your question? – Free Consulting Apr 03 '17 at 08:49
  • @DavidHeffernan: Actually, the OP's problem is simple, based on a misunderstanding. – MartynA Apr 03 '17 at 08:54
  • @MartynA No, the real problem is that the asker won't debug. For sure you and I can work out what the problem is by reading the code, but if the asker cannot, then debugging is required. The real underlying problem behind >99% of the questions here is an inability or unwillingness to debug. Asking a question here is a terrifically inefficient way to debug. Imagine if we could persuade the asker to learn how to debug, and learn to expect to be able to understand such problems by debugging? Then these people would be able to solve their own problems. Isn't that desirable to everybody? – David Heffernan Apr 03 '17 at 08:57
  • 3
    @DavidHeffernan: I completely agree. That's why I try - not always perfectly - to include in my answers a way into debugging the problem. It seems obvious with a q like this one that the OP doesn't know where to start. – MartynA Apr 03 '17 at 09:03

1 Answers1

3

If you rewrite your code like this:

var
  AField : TField;
begin
  AField := DBGrid2.SelectedField;
  Caption := AField.ToString;

  FDQuery2.Close;
  FDQuery2.SQL.Text := 'select column_name from information_schema.columns where table_schema="bases" and table_name = "'+DBGrid2.SelectedField.ToString+'"'; 
  FDQuery2.Open;
  ListBox2.Clear;
  FDQuery2.GetFieldNames(ListBox2.Items);

and run it, you should be able to see immediately what your problem is.

ToString does NOT return the value in the selected field of the grid - in your case it returns TStringField, which is the type of the field. not its string value. To get its string value from the table use DBGRid2.SelectedField.AsString.

There are a couple of other things I think are wrong with your code:

  1. Using a DBGrid's SelectedField to access a field's value. In general, that is ok to do, but since in your case you already know which field you need - Table_Name - what's the point of requiring the user to click it? Also, the user may select the wrong column.

    Better to use FDQuery2.FieldByName('Table_Name').AsString

  2. I bet your DBGrid2 is connected to FDQuery2 via its datasource. If so, basing your query on what's already displayed in DBGrid2 is asking for trouble unless you know what you are doing. If you are still working with the code in your other q, you would do better to get the Table_Name value from FDQuery1.

In any case, in my answer to your other q, Display database structure from Delphi (rad studio), I showed you how to set up a master-detail linkage between the two tables so that the second one, the table columns one, is automatically synced with the selected table in the first one.

Community
  • 1
  • 1
MartynA
  • 30,454
  • 4
  • 32
  • 73