cancel
Showing results for 
Search instead for 
Did you mean: 

Change Table name in RPT file at runtime (Programmatically)

getanoop1053-5
Discoverer
0 Kudos

The main objective is to rename/replace one or more Database tables currently present in RPT file with another table with same structure and field names, at run time.

I am trying to retrieve SQL query from RPT file , edit it (replacing a table with another or rename) and set this SQL query back to RPT file. After that calling Export method to create a pdf report out of it.

I am using C++\CLI with VS 2022.

Following is my code:

String^ fReportFile = gcnew String(aFileName);

gReport = gcnew ReportDocument();

//calling load report function of Reportdocument class

gReport->Load(fReportFile);

bool bConn= ConnectDataSource(gReport, aUserId, aPassword, aServer, aDatabase, false, aErrorLog);

gRowsetController = gReport->ReportClientDocument->RowsetController;

gGroupPath = gcnew GroupPathClass();

String^ strRPTSQLQuery;

String^ strSqlQuery = gRowsetController->GetSQLStatement(gGroupPath, strRPTSQLQuery); // here I am able to retrieve the query and modified it:

strSqlQuery = strSqlQuery->Replace("ItemMG", "Trade");

connectionString = "data source=" + ServerName+"; initial catalog = " + fConnectionInfo->DatabaseName + "; user id = " +

fConnectionInfo->UserID +"; password = " + fConnectionInfo->Password;

cnn = gcnew SqlConnection(connectionString);

cnn->Open();

SqlDataAdapter^ sqlDataAdapter = gcnew SqlDataAdapter(strSqlQuery, cnn);

System::Data::DataSet^ ds= gcnew System::Data::DataSet();

sqlDataAdapter->Fill(ds);

cnn->Close();

gReport->SetDataSource(ds); // till here it works and ds contains the correct query data

BUT after this when I call, Export method still uses initial query which was there in RPT instead of modified one:

//Export Report

gReport->Export();

Please help resolve this issue as I want to change the tables at runtime.

Thanks in advance,

Anoop

DellSC
Active Contributor
0 Kudos

Is your report based on a Command (SQL Select statement)? Or are you joining tables together in the Database Expert?

-Dell

Accepted Solutions (0)

Answers (1)

Answers (1)

getanoop1053-5
Discoverer
0 Kudos

Yes, joined tables in database expert.