First of all, I'm new to Delphi, and I checked all the relevant threads on here, but nothing seems to be working.
I'm just trying to fill a TComboBox with values of a column from a SQL Server database.
So basically, I have a VCL form and a DataModule, which holds an ADOConnection and an ADOQuery. The ADOConnection connects to my database (test connection says it's successful) and the ADOQuery connects to my ADOConnection. The VCL form and the DataModules are "connected".
(And actually here comes another problem: whenever I'm trying to enter SELECT name FROM Partners; in the properties of my ADOQuery and try to set it active, it keeps saying "Invalid object name 'Partners'". The SQL statement is correct, I tested it in SSMS and it retrieves the data without any error. But I'm not sure if it's the main issue here).
So basically, here's my code:
procedure TFormInsertNewItem.ComboBoxPartnersDropDown(Sender: TObject);
begin
with DataModule1 do
begin
ComboBoxPartners.Items.Clear;
ADOQueryFillPartners.SQL.Clear;
ADOQueryFillPartners.SQL.Add('SELECT name FROM Partners;');
ADOQueryFillPartners.Active:=true;
ADOQueryFillPartners.Open;
while not ADOQueryFillPartners.Eof do begin
ComboBoxPartners.Items.Add(ADOQueryFillPartners.FieldByName('name').AsString);
ADOQueryFillPartners.Next;
end;
ADOQueryFillPartners.Active:=false;
ADOQueryFillPartners.Free;
end;
end;
Whenever I click to drop it down (or maybe it would be better if this runs when the form is loaded, but I have no idea how can I exactly refer to that event), it should fill it up. Well, it won't. I tried it with TDBComboBox and TDBLookUpComboBox, with setting their datasource, but it only added 1 item to the combobox.
Can anyone help me with what I messed up and how can I get it to work, please?
ADOQueryFillPartners.Freewill have madeADOQueryFillPartnersa dangling pointer, so the next time you drop down the combo box, you will be using a dangling pointer. You should be using the form'sOnCreatehandler instead. Also don't forget to use theBeginUpdate; try ... finally EndUpdate end;pattern.Activeproperty of that component toTrue, what happens? (Don't forget to set it back toFalseafterward.) If you can't get that to work, you've not set up the connection string properly for your ADOConnection.