cancel
Showing results for 
Search instead for 
Did you mean: 

Changing the connection string programatically

Former Member
0 Kudos

Hello,

I'm developing a Windows Form appliction in managed C++ and I need to show a report with the CrystalReportViewer. I develop with CR for VS2010. They work fine, but my problem now is that I need to change the connection string used by the report at runtime. How can I change the connection string of the report? Some sample?

I have been searching internet for a while but to no avail.

Thanks!

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

I'm still trying to change the connection. I found that I need to change the connection string in the inner attributes of the connection. In some examples I found in KB and some search with google, they modify these attributes with a class called PropertyBag.

But these examples are in C# and VB, and I need in managed C++. I try directly this class but do not compile. Anyone know the equivalent class for ProtertyBag in c++?

Former Member
0 Kudos

Thank Don and Ludek. I have not solved yet my problem, but I think, with the information you suggest me, I managed to narrow down the problem to the ApplyLogOnInfo() function.

But that's another story, and I'll open a new thread.

0 Kudos

Hi Vicente,

This works for me:

boReportDocument = gcnew ReportDocument();

rptClientDoc = gcnew ReportClientDocument();

boReportDocument->Load("D:\\CPP Net 2010\\NET-CPP-Win2005_CRNET_CR115_Change_Record-Selection-Formula\\NoDateSQLParameter.rpt");

rptClientDoc = boReportDocument->ReportClientDocument;

rptClientDoc->DatabaseController->LogonEx("DBServer", "Database", "User", "Password");

It uses the RAS to set log on info... And assumes the report is using the connection info as when it was designed.

Don

Answers (1)

Answers (1)

former_member183750
Active Contributor
0 Kudos

I have no C++ samples, but C# samples are here:

http://wiki.sdn.sap.com/wiki/x/JQBmBQ

The above is for CR SDK. You could also use the InProc RAS SDK and this KBto get the code written out for you - in C# and VB...

- Ludek

Follow us on Twitter

Got Enhancement ideas? Try the SAP Idea Place

Share Your Knowledge in SCN Topic Spaces

Former Member
0 Kudos

I have been reviewing all the information I can found but nothing seem to work.

Here I paste my code (in C++), where I tried to change the connection at runtime:

CrystalDecisions::Shared::TableLogOnInfo ^li = gcnew CrystalDecisions::Shared::TableLogOnInfo;

for (int i=0;i<crReport->Database->Tables->Count;i++) {

      CrystalDecisions::Shared::ConnectionInfo ^ci = gcnew CrystalDecisions::Shared::ConnectionInfo;

      ci->ServerName="MYSERVER";

     ci->DatabaseName="MYDATABASE";

      ci->IntegratedSecurity=true;

      li=crReport->Database->Tables[i]->LogOnInfo;

      li->ConnectionInfo = ci;

      crReport->Database->Tables[i]->ApplyLogOnInfo(li);

}

After the for, when I attach the report to the viewer, they connect to the server and database the report was designed, but not to MYSERVER and MYDATABASE.

Have anyone an idea?

Thanks.

former_member183750
Active Contributor
0 Kudos

If yo are using Integrated Security, the report should not need any logon info. But do take a look at

this article.

- Ludek

0 Kudos

Search for ReplaceConnection in Kbase and it may be what you need to do.

Don

Former Member
0 Kudos

Thanks, I did, but no way! The connection still go to the original database 😞

Former Member
0 Kudos

Thanks Don, I did, but just get to be even more confused.

I think the problem is that, when designed the report, I connect to the database with a connection string. Then, in runtime, with the debugger of Visual Studio, I inspect the report object members, and found that the table's LogOnInfo->ConnectionInfo->Attributes, they appear a collection of attributes where my design's connection string appear. After applying the new connection (with ApplyLogOnInfo() of the above code), the new database and server appear correctly in the attributes, but still appear my original design's connection string!

May be the CR still use the original connection string.

I think I have to change also the connection string attribute, but however much I try, no way!