Modifying report variables in Reporting service 2008 R2

I've seen multiple times where we create a Read / Write Report variable (Group variables are striclty Read only) but not able to find a way to over write the value during the report execution phase.

Here is a way to do it.

Assume that you've created a report variable named "Reportvariable1".

All you need is a piece of custom code.

Public Function SetVariableValue(val as Microsoft.ReportingServices.ReportProcessing.OnDemandReportObjectModel.Variable)

val.Value = val.Value + 2

End Function

As simple as that. All i'm doing in the function is, take the report variable reference, add 2 to the existing value. That's it.

How you can call this function from your report item?


This will do the magic. Also note that in the above expression i'm just passing the variable reference and not its value.

You could always make some modifications in the way you want. For example, you want to modify the variable with a customized value and display the same in the textbox, here is what you need to do in the function.

Public Function SetVariableValue(val as Microsoft.ReportingServices.ReportProcessing.OnDemandReportObjectModel.Variable, newval As Integer) as Integer

val.Value = val.Value + newval

Return Val.Value

End Function

And call it from a textbox,

=Code.SetVariableValue(Variables!Reportvariable1, ReportItems!Textbox1.Value)

Hope this helps!


[All the posts are AS-IS with no warranty]

Comments (4)

  1. edy_almanza says:

    hi, i did that on report builder 2.0, but where run that report it gave an error message like this :

    There is an error on line 2 of custom code: [BC30526] propierty 'Value' is 'ReadOnly'.


    The definition of the report '' is invalid.


    how may I fix it… Help me Please.


  2. Selvar says:

    ReportBuilder 2.0 is only compatible with Reporting Service 2008 and not with Reporting Service 2008 R2. In Reporting service 2008, the variables were READ ONLY and only in Reporting service 2008 R2 and above it is READ, WRITE. So, with ReportBuilder 2.0 this cannot be accomplished.

    Try using ReportBuilder 3.0. But be aware of the fact that the reports you create from Report Builder 3.0 can only be saved on Reporting service 2008 R2 and above. Not on Reporting service 2008.



  3. Dan says:

    Is there a way to pass in all the variables, and access each one in code?  I have about 15 variables and most of them need to be used in a calculation that happens in the code. Passing a reference to the variables collection and pulling them out in code would be much easier.



  4. Selvar says:

    @Dan: I don't see anyway of achieving this. We've to rely on passing each paramter individually.



    [All the posts are AS-IS with no warranty]

Skip to main content