I am trying to get a datagrid to display data based on an ADOQuery to a MySQL database. The query runs and resturns the correct results, however when trying to display them in a datagrid, the datagrid displays the correct number of rows to be returned, but with no data in them.
The database contains three fields: RecordNumber (num), CustomerCode (cust_code) and TemplateNo (temp_no).
Here is my code:
---snip-------
unit viewmod;
interface
uses
... ADODB, DB;
type
...
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmViewMod: TfrmViewMod;
Global_DBConnection_String : String;
//ADOQuery to query database
lADOQuery : TADOQuery;
implementation
{$R *.dfm}
...
procedure TfrmViewMod.SearchByCustomer;
var
j : Integer;
strSearchString : String;
begin
//set global connection string
Global_DBConnection_String :=
'DRIVER={MySQL ODBC 3.51 Driver};' +
'SERVER=localhost;' +
'DATABASE=test;' +
'UID=root;' +
'PASSWORD=';
//validate whether data is in the field
if Trim(txtCustCode.Text) <> '' then
begin
//Assign customer code to search string variable to enable
//a search to be performed
strSearchString := '%' + UpperCase(txtCustCode.Text) + '%';
lADOQuery := TADOQuery.Create(nil);
with lADOQuery do
begin
//set connection string to use MySQL connection
ConnectionString := Global_DBConnection_String;
// Insert Record into Oracle DB using SQL
SQL.Text :=
'SELECT * FROM Lookup ' +
'WHERE cust_code LIKE '+
QuotedStr(strSearchString);
Open; //Open connection and query
//display results--> Problem starts
dsResults.DataSet := lADOquery;
dgdisplay.DataSource := dsResults;
dgdisplay.Refresh;
//-->problem ends --> Empty datagrid
//display column names
dgdisplay.Columns[0].FieldName := 'Record No.';
dgdisplay.Columns[1].FieldName := 'Customer Code';
dgdisplay.Columns[2].FieldName := 'Template No.';
end;
end;
end;
procedure TfrmViewMod.FormClose(Sender: TObject; var Action: TCloseAction);
begin
//close query
lADOQuery.Close;
end;
------snip---------
Now, I can loop through the dataset and go through all the records like this:
with lADOQuery.Recordset do
begin
if (RecordCount > 0) then //check whether records exist
begin
MoveFirst; //move to first record
//loop through dataset and retrieve data
for j := 1 to RecordCount do
begin
//Add search results to listbox
cboNumber.Items.Add(Fields['num'].Value);
//and so on...
MoveNext; //move to next record
end;
end;
end;
Can I do this and add the data to a datgrid? Or better yet can I set the datasource of the datagrid (like I have attempted earlier) and just display the data?
Thanks all.
------------------------------------
There's no place like 127.0.0.1
------------------------------------
The database contains three fields: RecordNumber (num), CustomerCode (cust_code) and TemplateNo (temp_no).
Here is my code:
---snip-------
unit viewmod;
interface
uses
... ADODB, DB;
type
...
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmViewMod: TfrmViewMod;
Global_DBConnection_String : String;
//ADOQuery to query database
lADOQuery : TADOQuery;
implementation
{$R *.dfm}
...
procedure TfrmViewMod.SearchByCustomer;
var
j : Integer;
strSearchString : String;
begin
//set global connection string
Global_DBConnection_String :=
'DRIVER={MySQL ODBC 3.51 Driver};' +
'SERVER=localhost;' +
'DATABASE=test;' +
'UID=root;' +
'PASSWORD=';
//validate whether data is in the field
if Trim(txtCustCode.Text) <> '' then
begin
//Assign customer code to search string variable to enable
//a search to be performed
strSearchString := '%' + UpperCase(txtCustCode.Text) + '%';
lADOQuery := TADOQuery.Create(nil);
with lADOQuery do
begin
//set connection string to use MySQL connection
ConnectionString := Global_DBConnection_String;
// Insert Record into Oracle DB using SQL
SQL.Text :=
'SELECT * FROM Lookup ' +
'WHERE cust_code LIKE '+
QuotedStr(strSearchString);
Open; //Open connection and query
//display results--> Problem starts
dsResults.DataSet := lADOquery;
dgdisplay.DataSource := dsResults;
dgdisplay.Refresh;
//-->problem ends --> Empty datagrid
//display column names
dgdisplay.Columns[0].FieldName := 'Record No.';
dgdisplay.Columns[1].FieldName := 'Customer Code';
dgdisplay.Columns[2].FieldName := 'Template No.';
end;
end;
end;
procedure TfrmViewMod.FormClose(Sender: TObject; var Action: TCloseAction);
begin
//close query
lADOQuery.Close;
end;
------snip---------
Now, I can loop through the dataset and go through all the records like this:
with lADOQuery.Recordset do
begin
if (RecordCount > 0) then //check whether records exist
begin
MoveFirst; //move to first record
//loop through dataset and retrieve data
for j := 1 to RecordCount do
begin
//Add search results to listbox
cboNumber.Items.Add(Fields['num'].Value);
//and so on...
MoveNext; //move to next record
end;
end;
end;
Can I do this and add the data to a datgrid? Or better yet can I set the datasource of the datagrid (like I have attempted earlier) and just display the data?
Thanks all.
------------------------------------
There's no place like 127.0.0.1
------------------------------------