Reporting Services Tips, Tricks and Funkiness
I've noticed over the last few months that people have been coming to me with questions about Sql Server Reporting Services... and I've been able to answer them. I had an issue with a Report Server myself a while ago and, when I asked who the Reporting Services guru was so I could get some help, was told "You are."
Anyway, the upshot of this is that I've realised that, even though I don't feel like it, I've picked up a fair amount of knowledge on the ins and outs of SSRS. Since I frequently try to Google reporting services issues I'm having and am unable to find answers easily I figured that putting some of the bits and pieces I've figured out into a blog post might help somebody :)
Here's a list of ideas that came to mind - I've either been asked how to do it by a colleague or I've had a client who required it and found it difficult to find a solution.
1 - Date formatting
If you have a string parameter coming in to your report (from a stored procedure for example) and need it displayed nicely, try using the expression:
=CDate(Fields!MeetingDate.Value).ToString("dd/MM/yyyy")
You will get minimum date values being displayed if the date is null for particular search criteria. If this is a problem for you, try using:
=IIF(IsDate(Fields!MeetingDate.Value),CDate(Fields!MeetingDate.Value).ToString("dd/MM/yyyy"),"")
Obviously in both these cases you can insert whatever format you'd like in place of "dd/MM/yyyy" :)
2 - IIF statement gotcha
Beware of using the IIF statement above - it works beautifully if the string is either null or a valid date but if the string is something like "tomorrow" (or even an empty string) then you will see "#Error" in your report. This is because IIF statements execute both paths when evaluating (they're not lazy enough!) so Reporting Services tries quite hard to convert "tomorrow" to a date and falls over when it can't. If you need to get around this try writing your own function (Report/Report Properties/Code) using more standard .NET IF statements and call it from your expression. Otherwise, just be aware and be careful when using IIF.
3 - Newlines in text boxes
If you're using an expression to build up a paragraph or two of text in a text box and need a new line, use this:
VBCRLF (Visual Basic Carriage Return Line Feed for those who're interested!)
E.g. ="This is line 1" + VBCRLF + "This isn't" will produce:
This is line 1
This isn't
Gotcha: If VBCRLF is the first or last thing in your text box, Reporting Services will ignore it (I have no idea why!). Make sure you put in one extra VBCRLF at the beginning or end if you're using them there.
4 - Checkboxes
I would have thought it would be fairly common to want to display a check box on a report which is checked based on a value in the dataset but it would appear that it's not so common as there is no control provided to handle this. If your client, like mine, really wants a checkbox displayed on a report, you'll need to use wingdings. Yes, you heard right, add a small text box to your report, set the font to wingdings and use something like the following expression:
=iif(Fields!BookingMade.Value = "1",chr(0254),"o")
Feel free to play around with different wingdings characters, but I found these two gave me a nice looking checkbox that stayed the same size whether it was checked or not!
5 - Alternating row colours
Large tables look better and are easier to read if you shade alternate rows. This is easily accomplished by adding the following to the BackgroundColour expression of your detail row (substituting your desired colours of course):
=iif(RowNumber(Nothing) Mod 2, "#eefafd", "White")
I had a situation recently where my client needed alternating row colours but, for a variety of reasons, the rows displayed on the report were all group header rows. Since the RowNumber resets at every group all my rows stayed white. Using the following expression gave me header rows with alternating colours (the IDNumber is what I was grouping on):
=iif(RunningValue(Fields!IDNumber.Value, CountDistinct, nothing) mod 2,"#eefafd","White")
Enough from me for today I think. For more on Reporting Services check out these websites that I've found helpful:
Rules to better SQL Reporting Services
Chris Hays' Reporting Services Sleazy Hacks
Are there any more useful tips and tricks out there? Or improvements to mine? Drop a comment :)
23 comments:
sweet trick with the checkbox/wingding item. i was banging my head with this as well, and this worked like a charm
This was SO helpful, that alternate row shading is really cool and I love the date formatting as well!
1) Date formating
You can also use the property Format in tables - e.g. "d", "D" for Date
3) Newlines
Chrw(13) & Chrw(10) works fine - also in Exports
Informative - Good job
vuf vuf
Cheers Jo!
I've just used 3 of those tips tonight!
Thats great, THANKS buddy :P
This was very helpful. You have saved my lots of hours and possible frustration! THANKS!
You are awesome!! I was braking my head to find a way to display a check box on my report, this was very helpful!, thanks
The checkbox was great! But have you tried exporting it to PDF.. There were no checks on the box,but in report viewer and excel it has.
Thanka buddy for that VBCRLF command!!! I was going crazy because I was not finding a way to do it !!
Too bad I didn't find your info about checkboxes before setting numerous checkboxes to IIF(field!name) = true because I couldn't get the IIF statement to recognize the actual 1 or 0 in the boolean field.
Now I need to figure out how to use the vbCrLf to put a break after x amount of characters.
Hello,
I'm trying to write a report with using a matrix showing totals and subtotals. And the problem is those total rows are not that easily distinguishable since they share the same format as regular table field.
Is there any way of changing the formatting for total fields? (i.e. changing background color) I tried using expression in background color property but couldn't find a correct formula.
THANK YOU - for taking the time to do this, for all those who don't but depend on great tips like this. appreciate it soooo much.
Thanks, you had two awesome tips that I was able to use!
The alternate grouping colors, and date formatting.
My report looks great now!
Cheers.
Kellie
Thanks for the Tips and Tricks..Really useful and time saving..
Hi,
Wonderful Posting.... Very Helpful.
Regards,
Sanjeev
such a nice command..it helps a lot thank for all
such a nice command..it is helping too much .thanks a lot
I would like to reuse the actual code of an expression in multiple matrices. For example I have a complex expression that calculates an amount in a matrix. This same calculation or amount is used in other matrices. I don't want to have to copy and paste the complex expression into other complex expressions in the other matrices but would like to be able to reference the actual expression in the other matrices. Do you know how I could accomplish this?
Here is an example of how to change the background color in a matrix depending upon the scope...
=iif(InScope("IntCred_RowGroup1"),
iif(InScope("IntCred_RowGroup2"),
"White",
"Lavender"),
"MistyRose"
)
@jpanzeca - yes you can reuse code in SSRS: you can write methods that get embedded in your report and can be used from anywhere within it. In SSRS 2005, you can find them in Report/Report Properties on the Code tab.
Really an excellent article. But checkbox using Wingdings is not supporting in PDF. So use the two image(check and uncheck) and assign any one to value of image control
Regarding the VBCRLF issue, I have also found that if I needed a line break at the beginning of my textbox (or multiple line breaks) that I don't want stripped out during exports, I set the top padding to a larger point size. For example, if I'm using a font size of 12 pt, I'll set the top padding property to 12 pt to simulate a single line break. If I need two line breaks, I'll set the top padding property to 24 pt (and so on). This doesn't get stripped out on export. You can apply the same method to the bottom padding property if you need line breaks at the end of your textbox.
Post a Comment