Skip to Content
0

Programmatically change ODBC Connection

Feb 16, 2017 at 08:02 AM

28

avatar image
Former Member

Hello,

I have to change at runtime odbc connection of my reports, and, obviously, I'm unable to do. I read many posts about this problem, and I applied this code to my solution, but it doesn't works. When I try to open my report I obtain something like "impossible to retrieve database data". I'm sure the new ODBC connection I want to use is correct. Someone can help?

Thanks

Mauro

            CrystalDecisions.CrystalReports.Engine.ReportObjects crReportObjects;
            CrystalDecisions.CrystalReports.Engine.SubreportObject crSubreportObject;
            CrystalDecisions.CrystalReports.Engine.ReportDocument crSubreportDocument;
            CrystalDecisions.CrystalReports.Engine.Database crDatabase;
            CrystalDecisions.CrystalReports.Engine.Tables crTables;
            TableLogOnInfo crTableLogOnInfo;


            CrystalDecisions.Shared.ConnectionInfo crConnectioninfo = new CrystalDecisions.Shared.ConnectionInfo();


            //pass the necessary parameters to the connectionInfo object
            crConnectioninfo.ServerName = "AAAA";
            crConnectioninfo.UserID = "";
            crConnectioninfo.Password = "";
            crConnectioninfo.DatabaseName = "bbbb";


            rd.DataSourceConnections[0].SetConnection(crConnectioninfo.ServerName, crConnectioninfo.DatabaseName, crConnectioninfo.UserID, crConnectioninfo.Password);


            //set up the database and tables objects to refer to the current report
            crDatabase = rd.Database;
            crTables = crDatabase.Tables;


            //loop through all the tables and pass in the connection info
            foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
            {
                crTableLogOnInfo = crTable.LogOnInfo;
                crTableLogOnInfo.ConnectionInfo = crConnectioninfo;
                crTable.ApplyLogOnInfo(crTableLogOnInfo);
            }


            //set the crSections object to the current report's sections
            CrystalDecisions.CrystalReports.Engine.Sections crSections = rd.ReportDefinition.Sections;


            //loop through all the sections to find all the report objects
            foreach (CrystalDecisions.CrystalReports.Engine.Section crSection in crSections)
            {
                crReportObjects = crSection.ReportObjects;
                //loop through all the report objects to find all the subreports
                foreach (CrystalDecisions.CrystalReports.Engine.ReportObject crReportObject in crReportObjects)
                {
                    if (crReportObject.Kind == ReportObjectKind.SubreportObject)
                    {
                        //you will need to typecast the reportobject to a subreport 
                        //object once you find it
                        crSubreportObject = (CrystalDecisions.CrystalReports.Engine.SubreportObject)crReportObject;


                        //open the subreport object
                        crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);


                        crSubreportDocument.DataSourceConnections[0].SetConnection(crConnectioninfo.ServerName, crConnectioninfo.DatabaseName, crConnectioninfo.UserID, crConnectioninfo.Password);


                        //set the database and tables objects to work with the subreport
                        crDatabase = crSubreportDocument.Database;
                        crTables = crDatabase.Tables;


                        //loop through all the tables in the subreport and 
                        //set up the connection info and apply it to the tables
                        foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
                        {
                            crConnectioninfo.ServerName = "AAAA";
                            crConnectioninfo.UserID = "";
                            crConnectioninfo.Password = "";
                            crConnectioninfo.DatabaseName = "bbbb";


                            crTableLogOnInfo = crTable.LogOnInfo;
                            crTableLogOnInfo.ConnectionInfo = crConnectioninfo;
                            crTable.ApplyLogOnInfo(crTableLogOnInfo);
                        }
                    }
                }
            }
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
Don Williams
Mar 21, 2017 at 07:58 PM
0

Hi Mauro,

Code looks like it should work.

What .NET SDK are you using?

See if this KBA works for you:

https://launchpad.support.sap.com/#/notes/1646498

Don

Share
10 |10000 characters needed characters left characters exceeded