How do you reference a field in the Embedded Code of an SSRS report

11,956

Solution 1

You have to pass it in as a parameter.

=Code.ToUSD(Fields!StandardCost.Value)

Solution 2

You do have two other alternatives to passing by parameter, though neither is very pretty.

(Beware! After I wrote the following paragraph I discovered defaults from queries are not supported in local processing mode so this first solution may not be viable for you as it was not for me.)

You can create a hidden report parameter with a default value set from a dataset, then reference this with the Report.Parameters!MyParam.Value syntax. You have to be careful when testing this, as (at least in BI studio 2005) the report parameters don't seem to get reliably re-initialised from the DB in the Preview tab.

Alternatively you can create a hidden textbox on the report with its text set from a dataset, and then reference the textbox from code. In this case you have to pass the ReportItems object as a parameter, but the slight advantage is that it is only ever one extra parameter. Be sure to strongly type the parameter when declaring it:

public Sub MyCustomCode(ri as ReportItems)

The code will work in BI studio without the type declaration but for me it caused errors with the report viewer control in local processing mode if 'as ReportItems' was not present.

In either case, this is only really useful for page level data, so functions for use in a table should still take parameters.

Share:
11,956
Seth Spearman
Author by

Seth Spearman

Self-taught. Geek.

Updated on June 18, 2022

Comments

  • Seth Spearman
    Seth Spearman almost 2 years

    Is there a proper way to reference the fields of a ssrs report from the embedded code of an ssrs report?

    When I try to use Fields!Program.Value I get the following error --

    There is an error on line 3 of custom code: [BC30469]
    Reference to a non-shared member requires an object reference.

    Upon googling I found you could reference the Parameters of a report by prepending Report. at the beginning. So I tried this Report.Fields.Program.Value.

    That results in the following error...

    There is an error on line 3 of custom code: [BC30456] 'Fields' is not a member of 'Microsoft.ReportingServices.ReportProcessing.ExprHostObjectModel.IReportObjectModelProxyForCustomCode'.

    So... in summary, is there a way to reference the fields from the embedded code. I figured out I could pass the field vals to the function itself but I would prefer to reference the fields directly.

    Seth

  • Seth Spearman
    Seth Spearman over 14 years
    Thanks...that's what I needed to know. Seth
  • Michael Eakins
    Michael Eakins almost 13 years
    It makes no sense to me that in reporting services, a parameter can be accessed directly while a field must be passed in. It would be more consistent if either, a parameter could not be accessed directly or a field could be accessed directly. Regardless, @gbn Thank you for the answer and @Seth Spearman thank you for the question.
  • ZygD
    ZygD almost 13 years
    @Michael Eakins: It makes sense for datasets. A parameter is single, scalar and discrete so can be accessed universally. If you have a 1000 data rows, how do you know which text box in which row belongs to the data in that row? This syntax makes it explicit and less error-prone
  • Michael Eakins
    Michael Eakins almost 13 years
    I was thinking to narrowly, your scenario makes it clear. Thanks!