Click here to Skip to main content
Click here to Skip to main content

Integrate Reporting Services with Silverlight and RIA Services

By , 28 Jul 2010
 

Introduction

One of the most wanted future features in Silverlight is the possibility to create business reports using Reporting Services. In this article, I will show how to use the existing Reporting Services technology with Silverlight and RIA Services combination.

Background

I assume that the reader has some basic to intermediate level knowledge in Silverlight and RIA Services and C#, also some knowledge about WCF and the Entity Framework.

Using the Code

The first thing to do is create a Silverlight Application in Visual Studio 2010. I used the Silverlight Business Application template.

In the ASP.NET project, I added an Entity Framework Model.

In this demo, I'm using the AdventureWorksLT database.

These are the entities we will use in this demo:

Build the project and add a Domain Service Class:

Here are all the entities selected to be added to the domain service:

Now we can add a new Report. In this case, we are using Reporting Services in Local mode.

In the report, when a Tablix component is added, a dataset is requested. In this case, the Choose the Dataset Wizard sees the domain context that we added before.

For this demo, I created a new class that I will use to bind to the report. There are cases in which that is preferable to do because we may need to do some extra processing before the data can be bound to the report:

using System;
using System.Collections.Generic;

using System.Linq;
using System.Web;

using System.ComponentModel.DataAnnotations;


namespace ReportingServicesDemo.Web
{
    public class Orders
    {
        public Orders()
        { }

        [Key]
        public int OrderDetailsID
        {
            get;
            set;
        }

        public int OrderID
        {
            get;
            set;
        }

        public string Product
        {
            get;
            set;
        }

        public int Quantity
        {
            get;
            set;
        }

        public decimal UnitPrice
        {
            get;
            set;
        }

        public decimal UnitPriceDiscount
        {
            get;
            set;
        }

        public decimal LineTotal
        {
            get;
            set;
        }
    }
}

In this class, I made a reference to the System.ComponentModel.DataAnnotations namespace that allows me to add a Key attributte to the OrderDetailsID property - that is a requirement for the class so the domain service can recognize it as an entity.

If we dive into the domain service code, we find this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using System.ServiceModel.DomainServices.EntityFramework;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;


//// Implements application logic using the AdventureWorksLT2008Entities context.
//// TODO: Add your application logic to these methods or in additional methods.
//// TODO: Wire up authentication (Windows/ASP.NET Forms)
///        and uncomment the following to disable anonymous access
//// Also consider adding roles to restrict access as appropriate.
// [RequiresAuthentication]
[EnableClientAccess()]
public class ADWLTDomainService : 
       LinqToEntitiesDomainService<AdventureWorksLT2008Entities>
{

Now I create a domain operation that exposes the Orders entity I just created in my custom class:

public IQueryable<Orders> GetOrderDetails(int OrderID)
{
    AdventureWorksLT2008Entities adw = new AdventureWorksLT2008Entities();

    var query = from c in adw.SalesOrderDetail.Include("Product")
                where 
                c.SalesOrderID == OrderID
                select new Orders { OrderID = OrderID, 
                OrderDetailsID = c.SalesOrderDetailID, Product = c.Product.Name, 
                Quantity = c.OrderQty, UnitPrice = 
                c.UnitPrice, UnitPriceDiscount = c.UnitPriceDiscount, 
                LineTotal = c.LineTotal };
   return query;
}

Here are some remarks. In this method, I'm using the Entity Framework context instead of the object context that the domain service declares, and exposing like the other select methods as IQueryable.

If we build the project again, the "Choose the DataSet Wizard" sees our custom method and all the fields exposed by the entity ready for our report.

Here is the report design together with the dataset available:

Now we add a WebForm to the web project in order to place the report using the ReportViewer control:

When we add the ReportViewer control, it creates an ObjectDataSource control.

Looking at the ObjectDataSource's properties, we can see the Select property of our custom method.

Looking at the SelectParameters collection, we see our custom method parameter.

Now in the Silverlight project, we add a DataGrid bound to the Sales Orders entity.

Each field in the Sales Order ID column is represented as a button just for this demo in order to pass the content value as a parameter on the click event. The idea is to call a popup window using the HtmlPage object, with HtmlPopup options from the System.Windows.Browser namespace.

HtmlPopupWindowOptions options = new HtmlPopupWindowOptions();
options.Left = 0;
options.Top = 0;
options.Width = 930;
options.Height = 800;
options.Menubar = false;
options.Toolbar = false;
options.Directories = false;
options.Status = false;

Button btn = sender as Button;

int OrderID =int.Parse(btn.Content.ToString());

string address = Application.Current.Host.Source.AbsoluteUri;
int i = address.IndexOf("/ClientBin/", 1);
string url = address.Substring(0, i);
url = url + "/RpDemoWebForm.aspx?OrderID=" + OrderID;

if (true == HtmlPage.IsPopupWindowAllowed)
    HtmlPage.PopupWindow(new Uri(url), "new", options);

We can obtain the current web address were the Silverlight XAP application is running, using the Application.Current.Host.Source.AbsoluteUri property. Later, we can create the URI associated with the ReportViewer's Web Form.

Finally, we can see our Order Details report.

Points of Interest

I hope this article can be helpful while we wait for the next Silverlight releases, and also can explore more options in business reporting.

History

  • 07-26-2010: Submitted to CodeProject.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ernesto Herrera
Web Developer Sigo S.A.
Venezuela Venezuela
Member
Ernesto Herrera is a Senior Developer, Architect with more than 12 years experience, actually working with Silverlight Line-of-Bussiness Applications, he holds a MCTS Web Application 2.0 Certification, and have a wide experience with sql server since the 6.5 version, on his free time he likes to play tennis with his wife,kids and friends, also enjoy playing Wii with his sons, he works at Sigo S.A., a retail business located at Margarita Island, Venezuela.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionPararmeter(OrderID) not being passedmemberIbrahim Islam26 Jul '12 - 1:25 
First of all, thank u very much for such a tutorial Big Grin | :-D
You did n00bs like me a very big favor
Next, I cannot seem to pass the Parameter to the report which in your tutorial is OrderID. Please suggest where I might be going wrong cause I followed your tutorial exactly.
Thanks.
QuestionHtmlPage.IsPopupWindowAllowedmemberSk8tz16 Apr '12 - 2:14 
Hi
 
I trying to get this solution to work, however HtmlPage.IsPopupWindowAllowed is always false. Any reason why this happens.
 
Using silverlight 4, IE9, vs2010 sp1
Sk8tZ

QuestionHow to manage the authentication/authorizationmemberMember 375780520 Feb '12 - 22:06 
Hi Ernesto,
 
Thanks for the excellent article.
 
How can we restrict the report page to authrized users only. e.g., if some unauthorized user types/copies the url of the reportwebform in the browser will the report be displayed to him? How can we redirect him to silverlight login page if he is not authenticated.
 
Are there any other options available ?(ofcourse not payable!!!)...
 
Does Silverlight 5.0 include any reporting solutions similar to this? Is this kind of reporting possible with Pivot viewer.
 
Thanks in advance.
 
Sohil Ravjani
AnswerRe: How to manage the authentication/authorizationmemberJP®17 Apr '12 - 7:47 
You can implement an authentication service and use session vars to control the report access.
Java / .NET Developer

QuestionDomainDataSource not available as data sourcememberGschweesen24 Jun '11 - 0:43 
Dear community
 
I followed this tutorial and everything works quite well! But when I try to add the tablix in an existing project and then want to choose my domain service as datasource, it is not available. The "New connection" wizard is shown instead... Why could that by? What could be the problem? I'm completely lost...
 
Thank you for your help!
Dave
AnswerRe: DomainDataSource not available as data sourcememberErnesto Herrera28 Jun '11 - 4:16 
Hi, in this kind of reports every table or tablix has its own datasource, two problems can be here: a namesapce problem or have to build the project, you can send a email at ernesth65@hotmail.com with some screenshots and the project folder to see,
 

Regards
GeneralStuck at last stepmemberVishwanatha J20 Apr '11 - 18:24 
I am not able to bind data to my datagrid in silverlight app.Can u pls tell how to do it?
GeneralRe: Stuck at last stepmemberErnesto Herrera28 Jun '11 - 4:17 
Simply drag from the data sources pane the entity that you want to bind and it creates the datagrid
GeneralThank you so much for sharingmembervsnewcomer16 Feb '11 - 3:30 
Thank you so much for this excellent article. I did exactly what you did and saw the parameter value passed to reportview but somehow did not pass into the IQueryble query so no data pull back. If i put default value in the parameter i can pull databack.
 
Will you please give me some suggestion how to figure out this problem?
 
Thank you very much!
GeneralRe: Thank you so much for sharingmembervsnewcomer16 Feb '11 - 3:42 
It is working now. I missed the code in the reportview pageload. Thank again for sharing this great article!!! Excellent Job!!! Smile | :)
Question[My vote of 1] Code does not runmemberlouiso9910 Dec '10 - 7:02 
I was not able to create the application using just the information in the article. The download also does not run. Is there more information available? :(
AnswerRe: [My vote of 1] Code does not runmemberErnesto Herrera16 Dec '10 - 10:52 
Hi, first you must have installed Silverlight 4 Tools with Visual Studio 2010 and the AdventureWorksLT Database, so you can check your web.config in order to configure your database connection, hope this helps.
GeneralIntegrate Reporting Servicesmembertechnette23 Sep '10 - 9:29 
Thank you Ernesto! This is absolutely wonderful... I'm going to take the weekend to explore how I can implement this with my current application.
GeneralRe: Integrate Reporting ServicesmemberErnesto Herrera28 Jun '11 - 4:19 
It's nice for me to know that can be useful for you
GeneralExcelente articulomemberjsoques20 Sep '10 - 16:54 
Paisano, excelente articulo.
GeneralRe: Excelente articulomemberErnesto Herrera28 Jun '11 - 4:20 
Gracias, esperando a escribir otro pronto
GeneralMy vote of 5memberAbhinav S7 Aug '10 - 18:19 
Good article for those who want to use SSRS for Silverlight.
GeneralRe: My vote of 5memberErnesto Herrera19 Aug '10 - 6:17 
Thank you very much for your appreciation
 
Best Regards
GeneralGood workmvpAbhishek Sur31 Jul '10 - 11:02 
Really appreciate your work.Rose | [Rose]
Abhishek Sur
Don't forget to click "Good Answer" if you like this Solution.
Visit My Website-->


www.abhisheksur.com

GeneralRe: Good workmemberErnesto Herrera2 Aug '10 - 4:14 
Thank you very much, is glad to hear that from a CodeProject MVP!
GeneralExcellentmembergoodnamecool31 Jul '10 - 6:19 
Excellent Thumbs Up | :thumbsup: !!!....Hold on Bolivar!.. Laugh | :laugh:
GeneralRe: ExcellentmemberErnesto Herrera28 Jun '11 - 4:21 
Thank you so much!, go ahead kinect man!
GeneralGOODmemberSachy28 Jul '10 - 5:25 
Hi, good report, thanks for share it with us! Big Grin | :-D
GeneralRe: GOODmemberErnesto Herrera28 Jul '10 - 5:36 
Thank you, hope to write more articles...

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 28 Jul 2010
Article Copyright 2010 by Ernesto Herrera
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid