Thursday, 2 August 2012

Crystal Report Example

I have a report and within that a sub-report.
Both are based on stored procedures which accept parameters.
In the link for the sub report I set it so the two parameters are linked.
I named them differently to avoid confusion - @Risk_Id_Parm is the main report parameter and @Risk_Id_Sub is the subreport parameter.
When I run against the database I developed against my code is fine.
When I change to point at another database I get an error telling me the subreport parameter is required.
Okaydoke... so I try and set the sub report parameter explicitly.
I get an error telling me the index is out of range ( the parameter ain't available for setting ).
Now, I can manually set the report to the live server at delivery so this isn't a huge problem for me right now but I'm doing something wrong.
I have patched the machine with the most recent hot fixes.
Any ideas?
================

ReportDocument rd, rdsub;

rd = new ReportDocument();

rd.Load(Server.MapPath("Reports\\Risk_Assessment.rpt"), CrystalDecisions.Shared.OpenReportMethod.OpenReportByTempCopy);

// Switch database

Sections crSections;

SubreportObject crSubreportObject;

ReportObjects crReportObjects;

Database crDatabase;

Tables crTables;

TableLogOnInfo crTableLogOnInfo;

crDatabase = rd.Database;

crTables = crDatabase.Tables;

ConnectionInfo crConn = new ConnectionInfo();

crConn.ServerName = DB.ServerString;

crConn.DatabaseName = "RiskMan";

crConn.UserID = DB.ServerUser;

crConn.Password = DB.ServerPassword;

crConn.IntegratedSecurity = false;

foreach (CrystalDecisions.CrystalReports.Engine.Table aTable in crTables)

{

Trace.Warn("Table is: " + aTable.Name);

crTableLogOnInfo = aTable.LogOnInfo;

crTableLogOnInfo.ConnectionInfo = crConn;

aTable.ApplyLogOnInfo(crTableLogOnInfo);

aTable.Location = crConn.DatabaseName + ".dbo." + aTable.Location.Substring(aTable.Location.LastIndexOf(".") + 1);

}

// THIS STUFF HERE IS FOR REPORTS HAVING SUBREPORTS

// set the sections object to the current report's section

crSections = rd.ReportDefinition.Sections;

// loop through all the sections to find all the report objects

foreach (Section crSection in crSections)

{

crReportObjects = crSection.ReportObjects;

//loop through all the report objects in there to find all subreports

foreach (ReportObject crReportObject in crReportObjects)

{

if (crReportObject.Kind == ReportObjectKind.SubreportObject)

{

crSubreportObject = (SubreportObject)crReportObject;

//open the subreport object and logon as for the general report

rdsub = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);

crDatabase = rdsub.Database;

crTables = crDatabase.Tables;

foreach (CrystalDecisions.CrystalReports.Engine.Table aTable in crTables)

{

Trace.Warn("sub Table is: " + aTable.Name);

crTableLogOnInfo = aTable.LogOnInfo;

crTableLogOnInfo.ConnectionInfo = crConn;

aTable.ApplyLogOnInfo(crTableLogOnInfo);

aTable.Location = crConn.DatabaseName + ".dbo." + aTable.Location.Substring(aTable.Location.LastIndexOf(".") + 1);

}

Trace.Warn(rdsub.Name);

}
}
}

//=======================

int i = rd.DataDefinition.ParameterFields.Count;

Trace.Warn("Number of report parameters:" + Convert.ToString(i));

Trace.Warn(rd.DataDefinition.ParameterFields[0].Name);

Trace.Warn(rd.DataDefinition.ParameterFields[0].ParameterFieldName);

Trace.Warn(rd.DataDefinition.ParameterFields[0].FormulaName);

 ParameterFieldDefinition paramField;

ParameterValues currentValues;

ParameterValues defaultValues;

paramField = rd.DataDefinition.ParameterFields["@Risk_Id_Parm"];

discreteParam.Value = Session["Default_Risk"];

currentValues = paramField.CurrentValues;

currentValues.Add(discreteParam);

paramField.ApplyCurrentValues(currentValues);

ParameterFieldDefinitions parameterFieldDefinitions = rd.DataDefinition.ParameterFields;

ParameterFieldDefinition parameterFieldDefinition = parameterFieldDefinitions["@Risk_Id_Sub","Risk_Assessment_Sub.rpt"];

// Above gives out of range exception


parameterFieldDefinition.CurrentValues.Clear();
parameterFieldDefinition.CurrentValues.Add(parameterFieldDefinition);
parameterFieldDefinition.ApplyCurrentValues(parameterFieldDefinition.CurrentValues);
crvRisk.ReportSource = rd;

crvRisk.DisplayGroupTree = false;

No comments:

Post a Comment