C# – Question about programmatically changing crystal reports

.net-3.5ccrystal-reportsvisual-studio-2008

General info: C#, VS2008, .NET 3.5

I've got a form with a crystal report viewer inside it, and i'm wanting to use the one form to display all of my crystal reports. I've got it so that i can programmatically change the report, the only problem is that i have to a new method for each crystal report. I'm wanting to create a single method that can take in the database name, server name, and then the CrystalReport. The code shown below is on the form that has the crystal reports viewer.

public void setReport(string databaseName, string serverName, ReportType report)
{
        crystalReportViewer1.ReportSource = report; 

        CrystalDecisions.Shared.ConnectionInfo crDbConnection = new CrystalDecisions.Shared.ConnectionInfo();
        crDbConnection.IntegratedSecurity = false;
        crDbConnection.DatabaseName = databaseName;
        crDbConnection.ServerName = serverName;
        crDbConnection.UserID = "userid";
        crDbConnection.Password = "password";
        CrystalDecisions.CrystalReports.Engine.ReportDocument oRpt = (CrystalDecisions.CrystalReports.Engine.ReportDocument)crystalReportViewer1.ReportSource;
        CrystalDecisions.CrystalReports.Engine.Database crDatabase = oRpt.Database;
        CrystalDecisions.Shared.TableLogOnInfo oCrTableLoginInfo;
        foreach (CrystalDecisions.CrystalReports.Engine.Table oCrTable in crDatabase.Tables)
        {
            oCrTableLoginInfo = oCrTable.LogOnInfo;
            oCrTableLoginInfo.ConnectionInfo = crDbConnection;
            oCrTable.ApplyLogOnInfo(oCrTableLoginInfo);
        }
    }

Best Answer

I used this:

 public enum ReportType : int //Must match actual rdlc filename!!
        {
            rptBankPaymentInstructions = 0,
            rptInstructionReconciliation = 1,
            rptMarketValue = 2,
            rptPortfolioInstructionsSummary = 3
        }

private void ShowRpt(ReportType type, string[] parametersValues, string[] parameterNames)
        {
            try
            {
                reportViewer1.LocalReport.DataSources.Clear();
                reportViewer1.LocalReport.ReportEmbeddedResource =  "ATPresentation.Reports." + type.ToString() + ".rdlc";
                //reportViewer1.LocalReport.ReportEmbeddedResource = "ATPresentation.Reports.rptMarketValue.rdlc";
                ReportEngineService.ReportService rService = new ATPresentation.ReportEngineService.ReportService();


                ReportDataSource rds = new ReportDataSource();


                switch (type)
                {
                    case ReportType.rptBankPaymentInstructions:
                        break;
                    case ReportType.rptMarketValue:
                        rds.Name = "Report_MarketValue";

                        ReportEngineService.Report.MarketValueDataTable dt = rService.GetMarketValueDt(Convert.ToInt32(parametersValues[0]), Convert.ToDateTime(parametersValues[1]));
                        rds.Value = dt;
                        //reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("Report_MarketValue",dt));
                        break;
                    default:
                        break;
                }

                List<ReportParameter> allParams = new List<ReportParameter>();

                for (int i = 0; i < parametersValues.Length; i++)
                {
                    Microsoft.Reporting.WinForms.ReportParameter rptParams = new ReportParameter( parameterNames[i], parametersValues[i]);
                    allParams.Add(rptParams);
                }

                reportViewer1.LocalReport.SetParameters(allParams);           
                reportViewer1.LocalReport.DataSources.Add(rds);
                reportViewer1.RefreshReport();
            }
            catch (Exception)
            {

                throw;
            }


        }
Related Topic