Home DADE

Change ADOConnection in runtime

edited June 2006 in DADE
Hello

I'm working on a websnap application with Delphi 6 and reportbuilder 7.02.
The database is SQL SERVER 2000.
I have a webdatamodule with a component ppreport1 and a TADOConnection
(connectRH). I use this ppreport1 to load templates.
Everything was fine until I decided to delete the TADOConnection component.
In fact, I want to recreate my connection just before printing.
I found several examples and tried something :

procedure TWebdatamodule1.ImprimeRecapitulatif(sender: TObject);
var
i: integer;
champ: string;
voirrem, DR: string;
Etat: string; Typedoc: string;
an, mois, jour: word;
ISQL: tdaSQL;
conn: TADOconnection;
begin
Etat := 'FicheRecap'; TypeDoc := '.pdf';

ppReport1.Template.CleanupInstance;

ppReport1.template.FileName := qualifyfilename('Specimen\' + Etat +
'.rtm');
ppReport1.template.loadfromfile;
ppReport1.AllowPrintToFile := true;
ppReport1.showAutoSearchDialog := false;

connectRH := TADOconnection.Create(nil);
connectRH.Name := 'connectRH';
connectRH.LoginPrompt := false;
connectRH.ConnectionString := qualifyfilename('ConnectRH.udl');
//------
if GetSQLObject(ppReport1, ISQL) then
begin
ISQL.DatabaseName := 'connectRH';
ISQL.DatabaseType := dtMSSQLServer;
ISQL.sqltype := sqSQL2;
SetSQLObject(ppReport1, ISQL);
// ISQL.Free;
end;
//------

ppReport1.DeviceType := 'Adobe Acrobat Document';
ppReport1.PrinterSetup.DocumentName := Webcontext.Session.SessionID + '_'
+ etat;
ppReport1.TextFileName := qualifyfilename('Etats\' +
Webcontext.Session.SessionID + '_' + etat + Typedoc);
ppReport1.ShowPrintDialog := false;
ppReport1.ShowCancelDialog := false;
ppReport1.print;
freeandnil(connectrh);
end;


procedure TWebdatamodule1.SetSQLObject(aReport: TppReport; aSQL: TdaSQL);
var
lDataModule: TdaDataModule;
lDataView: TdaDataView;
i, max: integer;
begin
{get the datamodule}
lDataModule := daGetDataModule(aReport);
if (lDataModule <> nil) then
begin
lDataView := lDataModule.DataViews[0];
if (lDataView <> nil) and (lDataView is TdaQueryDataView) then
TdaQueryDataView(lDataView).SQL := aSQL;
{
max := lDataModule.DataViewCount - 1;
for i := 0 to max do
begin
lDataView := lDataModule.DataViews[i];
if (lDataView <> nil) and (lDataView is TdaQueryDataView) then
TdaQueryDataView(lDataView).SQL := aSQL;
end; }
end;
end;

I have two errors.
1 - In SetSQLObject, if I use DataViews[0], the following message appears :
"TdaSession.GetDatabaseForName: No TADOConnection object found for specified
name, ConnectRH. A separate database connection is required for each thread.
"
Well, perhaps it's because I have 11 daQueryDataView in my report. Shall I
specify the connection for each object ?

2 - I tried to set the connection on every TdaQueryDataView like this in the
SetSQLObject function :
max := lDataModule.DataViewCount - 1;
for i := 0 to max do
begin
lDataView := lDataModule.DataViews[i];
if (lDataView <> nil) and (lDataView is TdaQueryDataView) then
TdaQueryDataView(lDataView).SQL := aSQL;
end;

but I have the error : "TdaLinkBroker.GetSQLObjects: No master SQL object
available. " on the line TdaQueryDataView(lDataView).SQL := aSQL;
I don't understand because the object is really a TdaQueryDataView. Do you
have any ideas ?

Thanks

Jerome

Comments

  • edited June 2006

    - The key here is that you said it was working before you deleted the
    TADOConnection component. I do not recommend doing that. You are just making
    things more complication. Instead, I recommend that you speficy the
    TADOConnection.ConnectionString property at run-time to connect to the
    appropriate database.

    - When using RB and ADO to create queries, the
    Designer.DataSettings.DatabaseName should reference the name of the
    ADOConnection component. The ADOConnection component must have the same
    Owner as the report. (In your code you used a nil owner - that will not
    work). Once the query has been created, the DatabaseName is saved as part of
    the query. When the query reloads, then the reference can be resolved if the
    ADOConnection object with the Name and the same Owner can be found.

    Best regards,

    Nard Moseley
    Digital Metaphors
    www.digital-metaphors.com
  • edited June 2006
    OK

    Thank you for these advice. I will try like this.

    Jerome

This discussion has been closed.