Thursday, May 14, 2009

SSRS - Custom code embedded in your report


In Reporting Services you will often need  to manipulate or dynamically format the  report data .  The built in  expressions  can do a lot of these, but most often you might want to format data or calculate values for which there are no expression readily available. This is where the built in custom code comes.
In one of my reports I had to calculate the Next month, having the current month and year as my input parameters.
You write your custom code in the report properties code window. Go to the report layout Select                                         Report ->  Report Properties  -> Code
        Public Function getNextDate(ByVal month As String, ByVal year As String) As String
Try
Dim dateNow As DateTime
dateNow   =month+ "-01-" + year
Dim nextMonth As DateTime
nextMonth  = dateNow  .AddMonths(1)
Return nextMonth

Catch ex As exception
Return ex.message
End Try
End Function

Now in the textbox where you want the next month displayed call the custom code 
="Next Month is  "+ code. getNextDate (Parameters!Month.Value,Parameters!Year.Value)