Skip to Content
0
Former Member
Feb 05, 2009 at 12:33 PM

Error exporting reports while viewing the exact same report works fine!

13 Views

Greetings,

We are using Visual Studio 2008 and Crystal Reports 2008 (with the service packs etc. installed). When setting the parameter values of a ReportDocument and then passing docuemtn to the viewer everything works 100%, when calling the ExportToDisk method I keep getting the ParameterFieldCurrentValueException being thrown. We are using a bespoke C# application as a reporting tool. All of the reports run off stored procedures. Please can someone help me?

I have included teh code for setting the database parameters, the parameter values themselves and the calling of the export function.

Thanks in advance,

Robert Lancaster


    private void SetReportLoginInfo(ReportDocument _rdReport)
    {
      try
      {
        // Must set the ConnectionInfo for all the tables in the report
        // Get the ConnectionInfo Object.
        TableLogOnInfo tlTableLogOnInfo = new TableLogOnInfo();

        foreach(Table ReportTable in _rdReport.Database.Tables)
        {
          if(ReportTable.Name == IMAGE_TABLE)  // Do not set login info for this table.
            continue;

          tlTableLogOnInfo = ReportTable.LogOnInfo;
      
          // Set the Connection parameters.
          tlTableLogOnInfo.ConnectionInfo.ServerName = dmDBManager.ServerName;  // Physical server name
          tlTableLogOnInfo.ConnectionInfo.DatabaseName = dmDBManager.DatabaseName;
          tlTableLogOnInfo.ConnectionInfo.UserID = dmDBManager.UserName;
          tlTableLogOnInfo.ConnectionInfo.Password = dmDBManager.Password;

          ReportTable.ApplyLogOnInfo(tlTableLogOnInfo);

          // Set the location for the report to the correct value.
          string[] sTemp = ReportTable.Location.Split('.');
          int iLength = sTemp.Length;
          if(iLength >= 2)
          {
            // Location will usually be in the form: DBName.Owner.Object
            // E.g. FleetData.dbo.crpt_AlarmsRpt;1
            // Change the name of the database to the correct value
            ReportTable.Location = dmDBManager.DatabaseName + "." + sTemp[iLength - 2] + 
              "." + sTemp[iLength - 1];
          }
          //          // DO NOT delete the following line of code!
          //          // For an explanation see:
          //          // http://support.crystaldecisions.com/forums/message.asp?fid=76&mid=94272
          //          ReportTable.Location = ReportTable.Location;
        }  // End foreach that steps through all the tables in the report

        // Must now check to see if there are any subreports in the report.
        // If there are, must change their login info as well.
        for(int iRptObj = 0; iRptObj <= _rdReport.ReportDefinition.ReportObjects.Count - 1; iRptObj++)
        {
          if(_rdReport.ReportDefinition.ReportObjects[iRptObj].Kind  == ReportObjectKind.SubreportObject)
          {
            string sName = (_rdReport.ReportDefinition.ReportObjects[iRptObj] as SubreportObject).SubreportName;
            // Create a new reportdocument from the subreport
            ReportDocument rdSubReport = _rdReport.OpenSubreport(sName);
            // Call function recursively
            SetReportLoginInfo(rdSubReport);
          }  // End if that checks for subreport object
        }  // End for that steps through all the objects in the report
      }
      catch(Exception ex)
      {
        string cError = "[ReportEngine][SetReportLoginInfo] " + ex.Message;
        Debug.WriteLine(cError);
        EventLogWriter.WriteErrorToEventlog(cError);
      }
    }  // End function SetReportLoginInfo

    private void AssignReportParams(ReportDocument _rdReport)
    {
      ParameterFieldDefinitions pfParameterFieldDefinitions = _rdReport.DataDefinition.ParameterFields;
      ParameterFieldDefinition pfParameterFieldDefinition;
      ParameterDiscreteValue pvParameterDiscreteValue;
      ParameterValues pvParameterValues;

      // Assign the values to all the params
      for (int iParam = 0; iParam < rpParams.Length; iParam++)
      {
        // Get the correct element from the array and clear any existing values
        pfParameterFieldDefinition = pfParameterFieldDefinitions[rpParams[iParam].ParamName]; // ,""];
        pvParameterValues = pfParameterFieldDefinition.CurrentValues;
        pvParameterValues.Clear();

        // Create a new value for the parameter and assign it
        pvParameterDiscreteValue = new ParameterDiscreteValue();
        pvParameterDiscreteValue.Value = rpParams[iParam].ParamValue;

        // Add the new value and apply it
        pvParameterValues.Add(pvParameterDiscreteValue);
        pfParameterFieldDefinition.ApplyCurrentValues(pvParameterValues);
      }

    }  // End function AssignReportParams

    private bool ExportReport(ReportDocument _rdReport,
                              CrystalDecisions.Shared.ExportFormatType _ftType,
                              out string _sError)
    {
      try
      {
        string sExpName = rpParameters.ReportExpName;

        DiskFileDestinationOptions diskOpts = ExportOptions.CreateDiskFileDestinationOptions();
        diskOpts.DiskFileName = sExpName;

        ExportOptions exportOpts = new ExportOptions();
        exportOpts.ExportFormatType = _ftType;
        exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
        exportOpts.ExportDestinationOptions = diskOpts;

        _rdReport.Export(exportOpts);

        //_rdReport.ExportToDisk(_ftType, sExpName);
        _sError = "";
        return true;
      }
      catch (ParameterFieldCurrentValueException)
      {
        _sError = ReportRunParam.sERROR_PARAMCURVALUE;  // THIS ALWAYS FIRES ON EXPORT
        return false;
      }

    }