Skip to Content
0
Former Member
Apr 21, 2010 at 04:10 PM

parameterfieldcurrentvalueexception error message

49 Views

Hi,

I have the following code working fine when the Crystal Report displays in the viewer. Now we want to send the document directly to PDF or Excel. I've added the memorystream code below the normal code and now I get the error message:

parameterfieldcurrentvalueexception was unhandled by user code - Missing parameter values.

I get the error on the line:

oStream = (MemoryStream)objReport.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

Please Help!

Thanks,

Darla

Code:

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

using System.Data.SqlClient;

using System.IO;

namespace CapNGoCSharp

{

public partial class _Default : System.Web.UI.Page

{

ReportDocument objReport = new ReportDocument();

ParameterField paramField = new ParameterField();

ParameterFields paramFields = new ParameterFields();

ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();

TableLogOnInfos crtableLogonInfos = new TableLogOnInfos();

TableLogOnInfo crtableLogoInfo = new TableLogOnInfo();

ConnectionInfo crConnectionInfo = new ConnectionInfo();

Tables crTables;

Sections crSections;

SubreportObject crSubreportObject;

ReportObjects crReportObjects;

ReportDocument crSubreportDocument;

Database crDatabase;

string sReportName;

int iReportID;

string ReportPath;

string userID;

string expenseReportID;

int iNumberOfParams;

bool notes = true;

protected void Page_Load(object sender, EventArgs e)

{

}

private void Page_Init(object sender, EventArgs e)

{

ConfigureCrystalReports();

}

private void ConfigureCrystalReports()

{

if (!IsPostBack || true)

{

//

// Get report info

//

userID = "04FC3B17-00B9-4F7C-88F2-D9888744F8B9";

expenseReportID = "FD5AAEBD-1B43-441A-B2FE-7A6E3911253E";

iReportID = 1;

bool Notes = true;

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings ["CngConnectionString"].ConnectionString);

// SqlConnection conn = new SqlConnection("Data Source=localhost;Database=Cng;Integrated Security=SSPI");

SqlCommand command = new SqlCommand("spGetReportInfo", conn);

command.CommandType = CommandType.StoredProcedure;

command.Parameters.Add("@ReportID", SqlDbType.Int).Value = iReportID;

SqlDataAdapter adapter = new SqlDataAdapter(command);

DataSet ds = new DataSet();

DataTable myDataTable = new DataTable();

//

// Fill the Adapter

//

adapter.Fill(ds, "Reports");

//

// Get the report name

// Need first record only

//

foreach (DataRow row in ds.Tables["Reports"].Rows)

{

sReportName = (string)row["ReportName"];

iNumberOfParams = (int)row["NumberOfParams"];

ReportPath = Server.MapPath(sReportName);

break;

}

//

//Get the Report Parameters

//

string[] aryParamType = new string[iNumberOfParams];

string[] aryParamVariable = new string[iNumberOfParams];

string[] aryParamName = new string[iNumberOfParams];

int i = 0;

foreach (DataRow row in ds.Tables["Reports"].Rows)

{

aryParamType<i> = (string)row["ParamType"];

if (String.Compare(aryParamType<i>, "text", true) == 0)

{

aryParamVariable<i> = (string)row["VariableName"];

aryParamName<i> = (string)row["ParamName"];

}

else

{

aryParamVariable<i> = (string)row["VariableName"];

aryParamName<i> = (string)row["ParamName"];

}

i = i + 1;

sReportName = (string)row["ReportName"];

iNumberOfParams = (int)row["NumberOfParams"];

}

ReportPath = Server.MapPath(sReportName);

//

// Set the Parameter Values

// Man, that's a lot of brackets!

//

{

int j = 0;

try

{

if (iNumberOfParams > 0)

{

paramField = new ParameterField();

paramField.Name = aryParamName[0];

for (j = 0; j < iNumberOfParams; j++)

{

if (j > 0)

{

paramField = new ParameterField();

paramField.Name = aryParamName[j];

}

if (aryParamVariable[j] == "userID")

{

paramDiscreteValue = new ParameterDiscreteValue();

paramDiscreteValue.Value = userID;

}

else

{

if (aryParamVariable[j] == "expenseReportID")

{

paramDiscreteValue = new ParameterDiscreteValue();

paramDiscreteValue.Value = expenseReportID;

}

else

{

if (aryParamVariable[j] == "notes") ;

{

paramDiscreteValue.Value = new ParameterDiscreteValue();

paramDiscreteValue.Value = Notes;

}

}

}

paramField.CurrentValues.Add(paramDiscreteValue);

paramFields.Add(paramField);

}

}

}

catch (InvalidCastException ex)

{

throw new Exception(ex.Message.ToString());

}

finally

{

}

}

}

//

// Add the Parameters

//

CrystalReportViewer1.ParameterFieldInfo = paramFields;

objReport.Load(ReportPath);

//

//Connection Info

//

crConnectionInfo.ServerName = ConfigurationManager.AppSettings["cngServer"];

crConnectionInfo.DatabaseName = ConfigurationManager.AppSettings["cngDatabase"];

crConnectionInfo.UserID = ConfigurationManager.AppSettings["cngUserID"];

crConnectionInfo.Password = ConfigurationManager.AppSettings["cngPassword"];

crConnectionInfo.IntegratedSecurity = true;

crConnectionInfo.Type = ConnectionInfoType.SQL;

crDatabase = objReport.Database;

//

// Logon to the tables

//

crTables = objReport.Database.Tables;

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

{

crtableLogoInfo = crtable.LogOnInfo;

crtableLogoInfo.ConnectionInfo = crConnectionInfo;

crtable.ApplyLogOnInfo(crtableLogoInfo);

crtable.Location = "dbo." + crtable.Location.Substring(crtable.Location.LastIndexOf(".") + 1);

}

//

// Logon to the subreports

//

crSections = objReport.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

//

crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);

crDatabase = crSubreportDocument.Database;

crTables = crDatabase.Tables;

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

{

crtableLogoInfo = aTable.LogOnInfo;

crtableLogoInfo.ConnectionInfo = crConnectionInfo;

aTable.ApplyLogOnInfo(crtableLogoInfo);

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

}

}

}

}

//

// Set the Report Source

//

CrystalReportViewer1.ReportSource = objReport;

// objReport.Refresh();

MemoryStream oStream; // using System.IO

oStream = (MemoryStream)objReport.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

Response.Clear();

Response.Buffer = true;

Response.ContentType = "application/pdf";

Response.BinaryWrite(oStream.ToArray());

Response.Flush();

Response.Close();

Response.End();

objReport.Dispose();