Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 9236

Updating Crystal Report Data Source at runtime

$
0
0

Hello Everyone,

 

I am using Microsoft Visual Studio 2010 with SAP Crystal Reports , version for Microsoft Studio (13.0.10) and  InterSystems CACHE 2014  as a back-end database.

 

I have a simple aspx page with a Crystal Report Viewer on it and I load a crystal report into that viewer using Report Document.

 

It works OK but I have a small issue.

 

If I try to load a report into the viewer which was created using a different Datasource than the one using which I try to load the report then it doesn't work. Basically I am unable to change the datasource of the report at runtime.

 

My crystal reports are created using Crystal Reports 2008 (and I have full version of it installed on my development machine).  I have this issue on both development and production machines.

 

E.g.  I have a Members.rpt which was created using datasource "ABC"  but when loading that report into Crystal Report viewer on my aspx page, I use datasource "XYZ"   , in this case "Crystal report viewer" doesn't throw any error but  doesn't load data either.

 

If I had created "Members.rpt" using datasource "XYZ"  then it would load data correctly when loaded into Crystal Report Viewer.

 

I also tried using  "Dataset"  as a Crystal report datasource.  i.e.  load a Dataset first by running a query and then assign datasource to report document datasource.  In this case it gives me

 

Failed to open the connection. Failed to open the connection. Members {D07CE43F-7143-40B6-B7BF-DD2749508B4F}.rpt


Here is the sample code, (sample code is not using Dataset)


//assemblies used

using InterSystems.Data.CacheClient;

using InterSystems.Data.CacheTypes;

 

ViewCrystalReport()

{

         

          string FullReportPath = "C:\Members.rpt";

           CrystalDecisions.CrystalReports.Engine.ReportDocument  rpt = new CrystalDecisions.CrystalReports.Engine.ReportDocument();

            rpt.Load(FullReportPath);

          

            ConnectionInfo connectionInfo = new ConnectionInfo();

            connectionInfo = GetConnectionStringDetails();

 

            for (int i = 0; i < CrystalReportViewer2.LogOnInfo.Count; i++)

            {

                CrystalReportViewer2.LogOnInfo[i].ConnectionInfo = connectionInfo;

            }

 

 

            Tables CRepTBLS = rpt.Database.Tables;

            foreach (CrystalDecisions.CrystalReports.Engine.Table repTable in CRepTBLS)

            {

                TableLogOnInfo login = repTable.LogOnInfo;

                login.ConnectionInfo = connectionInfo;

                repTable.ApplyLogOnInfo(login);

            }

           

            rpt.Refresh();

          

            //Simply display Report in Report Viewer

            CrystalReportViewer2.ReportSource = rpt;

            CrystalReportViewer2.DataBind();

 

}

 

 

private ConnectionInfo GetConnectionStringDetails()

    {

 

        string conn = "Server = localhost; Port = 1972; Namespace = XYZ; Password = XXX; User ID = XXXXXX"; // (string)ConfigurationManager.AppSettings["CacheConnectionString"])

 

        ConnectionInfo connectionInfo = new ConnectionInfo();

 

        connectionInfo.AllowCustomConnection = true;

 

        connectionInfo.IntegratedSecurity = false;

 

        string[] connDetails = conn.Split(';');

 

        for (int i = 0; i < connDetails.Length; i++)

        {

 

            string item = connDetails[i].Substring(0, connDetails[i].IndexOf(@"="));

 

            string value = connDetails[i].Substring(connDetails[i].IndexOf(@"=") + 1);

 

            switch (item.Trim().ToUpper())

            {

                case "NAMESPACE":

 

                        connectionInfo.ServerName = value.Trim();

                        break;

 

 

                case "USER ID":

 

                        connectionInfo.UserID = value.Trim();

                         break;

 

                case "PASSWORD":

 

                        connectionInfo.Password = value.Trim();

                        break;

 

                default:

                    break;

 

              }

        }

 

        return connectionInfo;

 

    }

 

Any help will be really appreciated.

 

Regards,

Utsavi


Viewing all articles
Browse latest Browse all 9236

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>