Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

 

I have a crystal report that fails on a formula field but I cannot see why.

 

Here is my code:

 

Public Function CreateReportPDF(ByVal dsReport As DataSet, ByVal CrystalReportPath As String, _
                                       ByVal reportSize As String, ByVal landScape As Boolean, _
                                           ByVal dsParameters As DataSet, ByVal filePath As String, _
                                               ByVal fileName As String) As Boolean
       Try
           'Load report including report path etc.
           _report.Load(CrystalReportPath, OpenReportMethod.OpenReportByTempCopy)
           _report.SetDataSource(dsReport)

           'Deal with any report parameters.           
           Dim crParameterDiscreteValue As ParameterDiscreteValue
           Dim crParameterFieldDefinitions As ParameterFieldDefinitions
           Dim crParameterFieldLocation As ParameterFieldDefinition
           Dim crParameterValues As ParameterValues

           'Get the report's parameters collection.           
           crParameterFieldDefinitions = _report.DataDefinition.ParameterFields

           For counter As Int32 = 0 To (dsParameters.Tables("ReportParameters").Rows.Count - 1)
               crParameterFieldLocation = crParameterFieldDefinitions.Item(dsParameters.Tables("ReportParameters").Rows(counter).Item("Key").ToString)
               crParameterValues = crParameterFieldLocation.CurrentValues
               crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
               crParameterDiscreteValue.Value = dsParameters.Tables("ReportParameters").Rows(counter).Item("Value").ToString
               crParameterValues.Add(crParameterDiscreteValue)
               crParameterFieldLocation.ApplyCurrentValues(crParameterValues)
           Next

           Dim CrExportOptions As ExportOptions
           Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions()
           Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()

           'Set the destination path and file name.
           CrDiskFileDestinationOptions.DiskFileName = "c:\Test.pdf"

           'Set export options.
           CrExportOptions = _report.ExportOptions

           With CrExportOptions
               'Set the destination to a disk file.
               .ExportDestinationType = ExportDestinationType.DiskFile
               'Set the format to PDF 
               .ExportFormatType = ExportFormatType.PortableDocFormat
               'Set the destination options to DiskFileDestinationOptions object.
               .DestinationOptions = CrDiskFileDestinationOptions
               .FormatOptions = CrFormatTypeOptions
           End With

           'Export the report.
           _report.Export()
       Catch ex As Exception
           Dim strEx As String = ex.ToString
       End Try
   End Function

 

The code throws an exception on the _report.Export() line.

 

The details of the exception are:

A string is required here.

Error in File C:\DOCUME~1\me\LOCALS~1\Temp\Snapshot {179546E4-7F1E-419C-944C-16C97E961CF0}.rpt:

Error in formula <fWeight>.

'iif({Snapshot.snapshot}=1,"",cstr({Snapshot.weight},3))'

A string is required here.

 

Here is the dataset:

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
 <Snapshot>
   <snapshot>1</snapshot>
   <stage>Drawing Office</stage>
   <fcode />
   <stcode />
   <weight>0</weight>
 </Snapshot>
 <Snapshot>
   <snapshot>1</snapshot>
   <stage>Planning</stage>
   <fcode />
   <stcode />
   <weight>0</weight>
 </Snapshot>
 <Snapshot>
   <snapshot>1</snapshot>
   <stage>Released to Sub Fab</stage>
   <fcode />
   <stcode />
   <weight>0</weight>
 </Snapshot>
 <Snapshot>
   <snapshot>1</snapshot>
   <stage>Sub Fabrication</stage>
   <fcode />
   <stcode />
   <weight>0</weight>
 </Snapshot>
 <Snapshot>
   <snapshot>1</snapshot>
   <stage>Own Treatment</stage>
   <fcode />
   <stcode />
   <weight>0</weight>
 </Snapshot>
 <Snapshot>
   <snapshot>1</snapshot>
   <stage>Treated</stage>
   <fcode />
   <stcode />
   <weight>0</weight>
 </Snapshot>
 <Snapshot>
   <snapshot>1</snapshot>
   <stage>Loaded</stage>
   <fcode />
   <stcode />
   <weight>0</weight>
 </Snapshot>
 <Snapshot>
   <snapshot>1</snapshot>
   <stage>In Packages</stage>
   <fcode />
   <stcode />
   <weight>0</weight>
 </Snapshot>
 <Snapshot>
   <snapshot>1</snapshot>
   <stage>In Containers</stage>
   <fcode />
   <stcode />
   <weight>0</weight>
 </Snapshot>
 <Snapshot>
   <snapshot>1</snapshot>
   <stage>At Site</stage>
   <fcode />
   <stcode />
   <weight>28.604</weight>
 </Snapshot>
 <Snapshot>
   <snapshot>1</snapshot>
   <stage>Erected</stage>
   <fcode />
   <stcode />
   <weight>0</weight>
 </Snapshot>
</NewDataSet>

 

If have hacked the field formula in the report to eliminate other problems to from this:

iif({Snapshot.snapshot}=1,"",cstr({Snapshot.weight},3))

To this:

iif({Snapshot.snapshot}=1,"Yep","Nope")

 

This gives me the same error, however:

iif(1=1,"Yep","Nope")

 

Does not error, So some how the value in the dataset are not coming through.

 

This has had me stumped all this afternoon. In the code I originally set the datasource on my report after I added the parameters. I changed this as the parameters values got wiped.

 

Has anyone got any suggestions as to what I am doing wrong?

 

I really struggle with crystal :confused:

 

Thanks, Dave.

Posted

Looks like it is treating Snapshot.snapshot as a string, try {Snapshot.snapshot}="1", this worked on a quick test but it also did not like the cstr({Snapshot.weight},3) had to change it to CStr({Snapshot.weight}).

 

I think crystal reports treats all xml fields as strings, so shouldn't need the CStr bit.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...