Feed Icon  

Contact

  • Bryant Likes
  • Send mail to the author(s) E-mail
  • twitter
  • View Bryant Likes's profile on LinkedIn
  • del.icio.us
Get Microsoft Silverlight
by clicking "Install Microsoft Silverlight" you accept the
Silverlight license agreement

Hosting By

Hot Topics

Tags

Open Source Projects

Archives

Ads

Writing Custom Code in SQL Server Reporting Services - Part II

Posted in Reporting Services at Wednesday, July 21, 2004 4:07 AM Pacific Daylight Time

Writing Custom Code in SQL Server Reporting Services - Part II

In my first article on Custom Code I went through the basics of using custom code. The goal of this article is to answer a question that was asked in the comments of the first article.

Sidney asked:

Could you elaborate on instance based classes and the procedures required to make this work.

So I looked into instance based code to see how it works and it is actually very simple.

Instance Based Code

In the first article we created a simple assembly called MyCustomAssembly that had a class called SayHello. For this article we will add an instance based method to that class and then call it from our report. First let's add the new method to the class. Below is the new code highlighted in bold.

[VB]
Public Class SayHello

  Private counter As Int32 = 0

  Public Function HelloWithCount() As String
    counter += 1
    Return String.Format("Hello! (for the {0} time)", counter)
  End Function

...

[C#]
public class SayHello
{

  private int counter = 0;

  public string HelloWithCount()
  {
    return string.Format("Hello! (for the {0} time)", ++counter);
  }

...

This new method will be instance based and will change the output based on how many times it has been called. So if we call HelloWithCount() three times we would expect the third call to output "Hello! (for the 3 time)".

After you've added the new method re-compile the project. You will need to copy the dll file again to the C:\Program Files\Microsoft SQL Server\80\Tools\Report Designer folder on your computer and overwrite the old version. Next open the report you're using to test the dll and add a reference to the dll (instructions here) if you haven't already done so in the last article. This time we also need to add an entry in the Class section.

In the class section add a new entry by clicking the the class name box and typing "MyCustomAssembly.SayHello" without the quotes. Next click in the instance box next to it and type "hello" without the quotes. This basically adds a variable named hello to the report of the type SayHello. So when the report is run a new instance of this object will be created (even if you don't access the object).

Now that we have an instance of our object we can make calls to it. Add three textboxes to your report and put "=Code.hello.HelloWithCount()" in each one. When you click the preview tab your report should run and you should see a different output in each box (1,2, and 3). You now have used instance based code in your report.

That's it! Hopefully you were able to get everything to work. Now you should have a basic understanding of how to use instance based code with reporting services. If you have a question leave a comment and I will try to address it. Thanks again for the great comments on my last article!

Saturday, July 31, 2004 9:44:00 PM (Pacific Daylight Time, UTC-07:00)
Hi
<br>how am'i doing deploy with the dll that used in develop environment ?
<br>how can i use external connection string to data source?
<br>
<br>thanks
<br>
<br>doron
Sunday, August 15, 2004 8:32:00 PM (Pacific Daylight Time, UTC-07:00)
Can we combine two tables each other?
alex
Friday, September 10, 2004 4:31:00 AM (Pacific Daylight Time, UTC-07:00)
HOw can I reference a report's run time attributes (not parameters!) within a custom assembly. I want to determine if the render format type is pdf, then set a custom global value and make navigation textbox's visible/non-visible based on that global value. Is it possible?
<br>
<br>thnks
<br>jim
jim
Saturday, October 09, 2004 4:21:00 AM (Pacific Daylight Time, UTC-07:00)
I seeing something really odd.. Just tried to deploy my custom assembly and I get this error message: Exception of type Microsoft.ReportingServices.ReportProcessing.ReportProcessingException was thrown.
<br>
<br>Any ideas?
<br>
derek
Tuesday, December 07, 2004 5:11:00 AM (Pacific Standard Time, UTC-08:00)
Thank you kindly for this website. I am having refreshing problems. We have available the autorefresh feature, but how could I refresh only onload. Can this be done via custom code. How?
<br>
<br>Will appreciate anything you can do for this.
<br>
<br>
<br>DotNetFranco@yahoo.com
franco
Tuesday, January 11, 2005 6:25:00 AM (Pacific Standard Time, UTC-08:00)
Hi Bryant,
<br>
<br>Thanks for this code. This is just what I needed!
<br>
<br>Regards,
<br>Q
Kjoebie
Saturday, December 09, 2006 3:27:58 PM (Pacific Standard Time, UTC-08:00)
is there any way to initialize values in the class SayHello. I want to add properties to this class, initialize the when the report loads, according to input from the use, and then use then for the life span of the report.
gilad
Monday, May 19, 2008 11:55:02 AM (Pacific Daylight Time, UTC-07:00)
great work man thx
Tuesday, June 17, 2008 3:38:01 AM (Pacific Daylight Time, UTC-07:00)
Hi Bryant,

I am creating some simple reports: 1 DataSet (Named "Revenue") and 1 table (table1) uses that DataSet. In the Development environment, I set the DataSource of the Revenue DataSet to the DataSource users most often want to run the report against. However, they can change the DataSouce. My problem is quite simple: How do I display the DataSource "Name" on the report? The users want to print these reports out, and "know" from whence database the report was generated against.

Any help would be greatly appreciated.

Steve
Steve
Comments are closed.