David David -4 years ago 242
Vb.net Question

Crystal Report formula field index changing on each new build

I've got a Crystal Report which I made in

CR2008
, which I'm printing from my
vb.net
application. In this report, I have an image and a formula field.

The image formula (Under Format Graphic > Picture > Graphic Location is set to be the formula field called
@imgLocation
.

In my
vb.net
code, I have the following block to select the saved file path from the database, then fill
@imgLocation
with this value, to set the image that is being shown.

Try
Dim logoPath As String = ds.Tables(0).Rows(0).Item("reportLogoPath") & ""

If logoPath = "" Then
MessageBox.Show("No logo path has been defined in the System Settings. Please set " & _
"one and try again.", "Load Report Failed", MessageBoxButtons.OK)
Exit Sub
Else
cReport.DataDefinition.FormulaFields.Item(2).Text = Chr(34) & logoPath & Chr(34)
End If

Catch ex As Exception
errorLog(ex)
End Try


I've seen this working twice. The image I want replaced the placeholder image when I had
FormulaFields.Item(2)
, but then I resized the image and it didn't work. I then changed the value of the index, and it worked with
FormulaFields.Item(4)
, but again stopped working when I resized the image.

Why is the index changing, and what is the way around it?

I tried using the named parameter option, but
FormulaFields.Item("@imgLocation")
gave me an error of


Invalid Index


EDIT

As per @Bugs request, this is the report showing the correct string and image

enter image description here

Then, after deleting and re-saving the image (But in a slightly bigger size), the parameter was passed correctly still, but the placeholder image was shown instead.

enter image description here

This makes me think now that it's to do with image size, however, re-sizing it back to the original size still didn't show it again.

EDIT 2

Could it be do to with the fact that I'm loading a new form which contains the report viewer, rather than opening it from the same form?

cReport.RecordSelectionFormula = selectionFormula
Me.Cursor = Cursors.Default
Dim f As New frmReportViewer(con, cReport, 0)
f.Show()

Private Sub frmReportViewer_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Try
setFormSizes(Me, con)
Me.Location = New Point((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2), 10)
Me.Text = "Report Viewer"

Dim logOnInfo As New TableLogOnInfo()
Dim i As Integer

For i = 0 To cReport.Database.Tables.Count - 1
logOnInfo.ConnectionInfo.DatabaseName = "comm_db"
logOnInfo.ConnectionInfo.Password = "Acplus2016!"
cReport.Database.Tables.Item(i).ApplyLogOnInfo(logOnInfo)
Next i

cReport.VerifyDatabase()

crViewer.ReportSource = cReport
crViewer.ToolPanelView = CrystalDecisions.Windows.Forms.ToolPanelViewType.None
crViewer.Zoom(87)

Catch ex As Exception
errorLog(ex)

End Try
End Sub

Answer Source

Please note my answer is addressing the part regarding adding parameters. For the moment the no image available placeholder is proving difficult to resolve on my side.

To add a parameter please follow these steps:

  1. Add a parameter within Crystal Reports called imageLocation:

    enter image description here

  2. After pressing OK you should have a parameter in your list like this:

    enter image description here

  3. The following code will pass a value to the parameter:

    Dim cReport As New ReportDocument
    cReport.Load("C:\Report.rpt")
    
    If cReport.ParameterFields.Item("imageLocation") IsNot Nothing Then
        cReport.SetParameterValue("imageLocation", "\\SAGE200FS\Sage\StockImages\sc002.jpg")
    End If
    
    reportViewer.ReportSource = cReport
    
  4. This is the output (I have added the parameter onto the report):

    enter image description here

Note that if you are passing parameters and they are shown on the report, make sure you don't have cReport.VerifyDatabase() in place. This often throws up a dialog box asking for the parameter values.

Screenshot of dialog box:

enter image description here

By not calling cReport.VerifyDatabase() this will stop the dialog box from showing.

Now through Crystal Reports you can set the Graphic Location using a parameter. To do this, add the parameter to the report and suppress the field (as you don't want to see it). Then you can add the Report Field as the location.

  1. Add parameter to report and suppress:

    enter image description here

  2. Add Report Field as the Graphic Location for the OLE Object:~=

    enter image description here

  3. Run the report and input the parameter. This will change the image accordingly. See both screenshots:

SC001::

enter image description here

INT4303:

enter image description here

This however does not seem to work through VB. The ReportViewer does not handle the change of images. Instead you are left with the default image. I'm unsure why this is the case and I'm guessing it's a bug with Crystal. If anyone knows of why this happens, feel free to comment.

EDIT

After discussing the smaller points in chat, we seem to have worked out that the issue arose because of the file path. Saving it to the database as \\server\...\...\image.png was not displaying, but after changing it to Z:\...\...\image.png it has worked fine every time. It was because of the file path, all along.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download