Está en la página 1de 17

9/20/2010 Integrate Reporting Services with Silverli…

4.88 / 5, 8 votes
Web Development » Silverlight » HowTo
First Posted 28 Jul 2010

Integrate Reporting Services with Views


Bookmarked
5,081
27 times
Silverlight and RIA Services Licence C POL

By Ernesto Herrera | 28 Jul 2010 ASP.NET, Windows, Dev, WC F,


Intermediate, Silverlight,
C #4.0
Integrate Reporting Services with your Silverlight Line-of-
Business applications.

Download sourc e - 801 KB

Introduction
One of the most wanted future features in Silverlight is the possibility to c reate business reports using
Reporting Servic es. In this artic le, I will show how to use the existing Reporting Servic es tec hnology
with Silverlight and RIA Servic es c ombination.

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

Using the Code


T he first thing to do is c reate a Silverlight Applic ation in Visual Studio 2010. I used the Silverlight
Business Applic ation template.

codeproject.com/KB/…/SLReporting.asp… 1/17
9/20/2010 Integrate Reporting Services with Silverli…

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

codeproject.com/KB/…/SLReporting.asp… 2/17
9/20/2010 Integrate Reporting Services with Silverli…

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

codeproject.com/KB/…/SLReporting.asp… 3/17
9/20/2010 Integrate Reporting Services with Silverli…

codeproject.com/KB/…/SLReporting.asp… 4/17
9/20/2010 Integrate Reporting Services with Silverli…

T hese are the entities we will use in this demo:

codeproject.com/KB/…/SLReporting.asp… 5/17
9/20/2010 Integrate Reporting Services with Silverli…

Build the projec t and add a Domain Servic e Class:

codeproject.com/KB/…/SLReporting.asp… 6/17
9/20/2010 Integrate Reporting Services with Silverli…

Here are all the entities selec ted to be added to the domain servic e:

codeproject.com/KB/…/SLReporting.asp… 7/17
9/20/2010 Integrate Reporting Services with Silverli…

Now we c an add a new Report. In this c ase, we are using Reporting Servic es in Loc al mode.

codeproject.com/KB/…/SLReporting.asp… 8/17
9/20/2010 Integrate Reporting Services with Silverli…

In the report, when a T ablix c omponent is added, a dataset is requested. In this c ase, the Choose the
Dataset Wizard sees the domain c ontext that we added before.

For this demo, I c reated a new c lass that I will use to bind to the report. T here are c ases in whic h that
is preferable to do bec ause we may need to do some extra proc essing before the data c an be bound to
the report:

using System;
using System.Collections.Generic;

codeproject.com/KB/…/SLReporting.asp… 9/17
9/20/2010 Integrate Reporting Services with Silverli…

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;
}
}
}

codeproject.com/KB/…/SLReporting.asp… 10/17
9/20/2010 Integrate Reporting Services with Silverli…
In this c lass, I made a referenc e to the System.ComponentModel.DataAnnotations namespac e
that allows me to add a Key attributte to the OrderDetailsID property - that is a requirement for
the c lass so the domain servic e c an rec ognize it as an entity.

If we dive into the domain servic e c ode, 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 c reate a domain operation that exposes the Orders entity I just c reated in my c ustom c lass:

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 c ontext instead of the objec t
c ontext that the domain servic e dec lares, and exposing like the other selec t methods as IQueryable.

If we build the projec t again, the "Choose the DataSet Wizard" sees our c ustom method and all the
fields exposed by the entity ready for our report.

codeproject.com/KB/…/SLReporting.asp… 11/17
9/20/2010 Integrate Reporting Services with Silverli…

Here is the report design together with the dataset available:

Now we add a WebForm to the web projec t in order to plac e the report using the ReportViewer
c ontrol:

codeproject.com/KB/…/SLReporting.asp… 12/17
9/20/2010 Integrate Reporting Services with Silverli…

When we add the ReportViewer c ontrol, it c reates an ObjectDataSource c ontrol.

Looking at the ObjectDataSource's properties, we c an see the Selec t property of our c ustom
method.

codeproject.com/KB/…/SLReporting.asp… 13/17
9/20/2010 Integrate Reporting Services with Silverli…

Looking at the SelectParameters c ollec tion, we see our c ustom method parameter.

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

codeproject.com/KB/…/SLReporting.asp… 14/17
9/20/2010 Integrate Reporting Services with Silverli…

Eac h field in the Sales Order ID c olumn is represented as a button just for this demo in order to pass
the c ontent value as a parameter on the c lic k event. T he idea is to c all a popup window using the
HtmlPage objec t, with HtmlPopup options from the System.Windows.Browser namespac e.

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 c an obtain the c urrent web address were the Silverlight XAP applic ation is running, using the
Application.Current.Host.Source.AbsoluteUri property. Later, we c an c reate the URI
assoc iated with the ReportViewer's Web Form.
codeproject.com/KB/…/SLReporting.asp… 15/17
9/20/2010
assoc iated with the ReportViewer's Integrate
Web Form.Reporting Services with Silverli…

Finally, we c an see our Order Details report.

Points of Interest
I hope this artic le c an be helpful while we wait for the next Silverlight releases, and also c an explore
more options in business reporting.

History
07- 26- 2010: Submitted to CodeProjec t.

License
T his artic le, along with any assoc iated sourc e c ode and files, is lic ensed under T he Code Projec t Open
Lic ense (CPOL)

About the Author

Ernesto Herrera Ernesto Herrera is a Senior Developer, Arc hitec t with more than 12 years
experienc e, ac tually working with Silverlight Line- of- Bussiness Applic ations, he
holds a MCT S Web Applic ation 2.0 Certific ation, and have a wide experienc e
Web Developer with sql server sinc e the 6.5 version, on his free time he likes to play tennis
Sigo S.A. with his wife,kids and friends, also enjoy playing Wii with his sons, he works at
Venezuela Sigo S.A., a retail business loc ated at Margarita Island, Venezuela.

Member

codeproject.com/KB/…/SLReporting.asp… 16/17
9/20/2010 Integrate Reporting Services with Silverli…

Follow on Twitter

Comments and Discussions


8 messages have been posted for this artic le Visit
http://www.codeproject.com/KB/silverlight/SLReporting.aspx to post and view c omments on
this artic le, or c lic k here to get a print view with messages.

P e rmaL ink | P rivac y | T erms of U s e C opyright 2 0 1 0 by E rnes to H errera


L as t U pdated: 2 8 J ul 2 0 1 0 E verything els e C opyright © C ode P rojec t, 1 9 9 9 - 2 0 1 0
Web2 4 | A dvertis e on the C ode P rojec t

codeproject.com/KB/…/SLReporting.asp… 17/17

También podría gustarte