D
Dennis Johansson
Hi all!
I have a page that is supposed to export a crystal report. The following
exception occurs when i try to export a report that contains subreports and
where both main and subreport get their values from stored procedures. I use
subreport links to pass the parameters from main report to subreport.
Exception:
CrystalDecisions.CrystalReports.Engine.ParameterFieldCurrentValueException:
Missing parameter field current value.
I'm about to lose my wits over this, and would really appreciate any help
provided.
Dennis
This is the code I use to perform this. (page is called from another page
where appropriate Session variables are assigned):
public class ReportViewer : System.Web.UI.Page
{
#region Private Members
private ReportDocument _rptDoc;
protected string[] ReportParams = null;
protected string[] ParamValues = null;
protected string ReportName;
#endregion
private void Page_Load(object sender, System.EventArgs e)
{
// Extract sessioninfo
ReportName = (string)Session["REPORT_NAME"];
ReportParams = (string[])Session["REPORT_REPORTPARAMS"];
ParamValues = (string[])Session["REPORT_PARAMVALUES"];
// Load the report
LoadReport();
// Set logon info
PassSQLCredentials("SERVER", "DB","USER", "PWD");
// Load the params if they are supplied
if(ParamValues != null && ReportParams != null)
{
PassParams();
}
Export();
}
protected void LoadReport()
{
string filePath = Server.MapPath(ReportName);
_rptDoc = new ReportDocument();
_rptDoc.Load(filePath);
_rptDoc.Refresh();
}
protected void PassParams()
{
int rptParamIndex = 0;
string reqParamValue;
// compare number of params
if(ParamValues.Length == ReportParams.Length)
{
// loop thru the supplied parameters
foreach(string rptParam in ReportParams)
{
// get the requested parameter
// check if it exists, throw exception if not...
reqParamValue = ParamValues[rptParamIndex];
// add the parameter to reportdocument
AddReportParameter(rptParam, reqParamValue);
// increase index
rptParamIndex++;
}
}
else
throw new Exception("Argument mismatch in parametercollection");
}
protected void Export()
{
System.IO.Stream st = null;
if(this._rptDoc == null)
throw new Exception("Impossible to export a non-existing report");
try
{
ExportOptions exp = new ExportOptions();
switch ((string)Session["REPORT_TYPE"])
{
case "pdf":
exp.ExportFormatType = ExportFormatType.PortableDocFormat;
break;
case "rtf":
exp.ExportFormatType = ExportFormatType.RichText;
break;
case "word":
exp.ExportFormatType = ExportFormatType.WordForWindows;
break;
}
exp.FormatOptions = new PdfRtfWordFormatOptions();
ExportRequestContext req = new ExportRequestContext();
req.ExportInfo = exp;
_rptDoc.FormatEngine.PrintOptions.PaperSize = PaperSize.PaperA4;
_rptDoc.FormatEngine.PrintOptions.PaperOrientation =
PaperOrientation.Landscape;
st = _rptDoc.FormatEngine.ExportToStream(req);
}
catch(Exception ex)
{
throw new Exception("Failed to export report to browser", ex);
}
try
{
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "application/pdf";
byte[] b = new byte[st.Length];
st.Read(b,0,(int) st.Length);
Response.BinaryWrite(b);
Response.End();
}
catch (Exception)
{
}
}
private void AddReportParameter(string paramFieldName, string
paramFieldValue)
{
if(this._rptDoc == null)
throw new Exception("Impossible to set parameter on non-existing
report");
try
{
// set parametervalue for report
Trace.Write(paramFieldName,paramFieldValue);
_rptDoc.SetParameterValue(paramFieldName, paramFieldValue);
}
catch(Exception ex)
{
throw new Exception("Failed to set parameter on report", ex);
}
}
// Used to set logon-credentials for the stored procedures used in the
report.
protected void PassSQLCredentials(string serverName, string databaseName,
string userId, string password)
{
if(this._rptDoc == null)
throw new Exception("Impossible to set logon-credentials on non-existing
report");
try
{
// create the logoninfo-structure
TableLogOnInfo logOnInfo = new TableLogOnInfo ();
logOnInfo.ConnectionInfo.ServerName = serverName;
logOnInfo.ConnectionInfo.DatabaseName = databaseName;
logOnInfo.ConnectionInfo.UserID = userId;
logOnInfo.ConnectionInfo.Password = password;
//Set logoninfo for main report
foreach(CrystalDecisions.CrystalReports.Engine.Table tbl in
_rptDoc.Database.Tables)
{
// set logon credentials...
tbl.ApplyLogOnInfo(logOnInfo);
}
//Set logoninfo for subreports
foreach(ReportObject rptObj in _rptDoc.ReportDefinition.ReportObjects)
{
if(rptObj.Kind == ReportObjectKind.SubreportObject)
{
SubreportObject subObj = (SubreportObject)rptObj;
string subReportName = subObj.SubreportName;
ReportDocument rptSub = _rptDoc.OpenSubreport(subReportName);
foreach(CrystalDecisions.CrystalReports.Engine.Table subtbl in
rptSub.Database.Tables)
{
// set logon credentials...
subtbl.ApplyLogOnInfo(logOnInfo);
}
}
}
}
catch(Exception ex)
{
throw new Exception("Failed to set logon-credentials for report", ex);
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
I have a page that is supposed to export a crystal report. The following
exception occurs when i try to export a report that contains subreports and
where both main and subreport get their values from stored procedures. I use
subreport links to pass the parameters from main report to subreport.
Exception:
CrystalDecisions.CrystalReports.Engine.ParameterFieldCurrentValueException:
Missing parameter field current value.
I'm about to lose my wits over this, and would really appreciate any help
provided.
Dennis
This is the code I use to perform this. (page is called from another page
where appropriate Session variables are assigned):
public class ReportViewer : System.Web.UI.Page
{
#region Private Members
private ReportDocument _rptDoc;
protected string[] ReportParams = null;
protected string[] ParamValues = null;
protected string ReportName;
#endregion
private void Page_Load(object sender, System.EventArgs e)
{
// Extract sessioninfo
ReportName = (string)Session["REPORT_NAME"];
ReportParams = (string[])Session["REPORT_REPORTPARAMS"];
ParamValues = (string[])Session["REPORT_PARAMVALUES"];
// Load the report
LoadReport();
// Set logon info
PassSQLCredentials("SERVER", "DB","USER", "PWD");
// Load the params if they are supplied
if(ParamValues != null && ReportParams != null)
{
PassParams();
}
Export();
}
protected void LoadReport()
{
string filePath = Server.MapPath(ReportName);
_rptDoc = new ReportDocument();
_rptDoc.Load(filePath);
_rptDoc.Refresh();
}
protected void PassParams()
{
int rptParamIndex = 0;
string reqParamValue;
// compare number of params
if(ParamValues.Length == ReportParams.Length)
{
// loop thru the supplied parameters
foreach(string rptParam in ReportParams)
{
// get the requested parameter
// check if it exists, throw exception if not...
reqParamValue = ParamValues[rptParamIndex];
// add the parameter to reportdocument
AddReportParameter(rptParam, reqParamValue);
// increase index
rptParamIndex++;
}
}
else
throw new Exception("Argument mismatch in parametercollection");
}
protected void Export()
{
System.IO.Stream st = null;
if(this._rptDoc == null)
throw new Exception("Impossible to export a non-existing report");
try
{
ExportOptions exp = new ExportOptions();
switch ((string)Session["REPORT_TYPE"])
{
case "pdf":
exp.ExportFormatType = ExportFormatType.PortableDocFormat;
break;
case "rtf":
exp.ExportFormatType = ExportFormatType.RichText;
break;
case "word":
exp.ExportFormatType = ExportFormatType.WordForWindows;
break;
}
exp.FormatOptions = new PdfRtfWordFormatOptions();
ExportRequestContext req = new ExportRequestContext();
req.ExportInfo = exp;
_rptDoc.FormatEngine.PrintOptions.PaperSize = PaperSize.PaperA4;
_rptDoc.FormatEngine.PrintOptions.PaperOrientation =
PaperOrientation.Landscape;
st = _rptDoc.FormatEngine.ExportToStream(req);
}
catch(Exception ex)
{
throw new Exception("Failed to export report to browser", ex);
}
try
{
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "application/pdf";
byte[] b = new byte[st.Length];
st.Read(b,0,(int) st.Length);
Response.BinaryWrite(b);
Response.End();
}
catch (Exception)
{
}
}
private void AddReportParameter(string paramFieldName, string
paramFieldValue)
{
if(this._rptDoc == null)
throw new Exception("Impossible to set parameter on non-existing
report");
try
{
// set parametervalue for report
Trace.Write(paramFieldName,paramFieldValue);
_rptDoc.SetParameterValue(paramFieldName, paramFieldValue);
}
catch(Exception ex)
{
throw new Exception("Failed to set parameter on report", ex);
}
}
// Used to set logon-credentials for the stored procedures used in the
report.
protected void PassSQLCredentials(string serverName, string databaseName,
string userId, string password)
{
if(this._rptDoc == null)
throw new Exception("Impossible to set logon-credentials on non-existing
report");
try
{
// create the logoninfo-structure
TableLogOnInfo logOnInfo = new TableLogOnInfo ();
logOnInfo.ConnectionInfo.ServerName = serverName;
logOnInfo.ConnectionInfo.DatabaseName = databaseName;
logOnInfo.ConnectionInfo.UserID = userId;
logOnInfo.ConnectionInfo.Password = password;
//Set logoninfo for main report
foreach(CrystalDecisions.CrystalReports.Engine.Table tbl in
_rptDoc.Database.Tables)
{
// set logon credentials...
tbl.ApplyLogOnInfo(logOnInfo);
}
//Set logoninfo for subreports
foreach(ReportObject rptObj in _rptDoc.ReportDefinition.ReportObjects)
{
if(rptObj.Kind == ReportObjectKind.SubreportObject)
{
SubreportObject subObj = (SubreportObject)rptObj;
string subReportName = subObj.SubreportName;
ReportDocument rptSub = _rptDoc.OpenSubreport(subReportName);
foreach(CrystalDecisions.CrystalReports.Engine.Table subtbl in
rptSub.Database.Tables)
{
// set logon credentials...
subtbl.ApplyLogOnInfo(logOnInfo);
}
}
}
}
catch(Exception ex)
{
throw new Exception("Failed to set logon-credentials for report", ex);
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}