Datasource for RDL reports with ReportViewer

Joao de Araujo picture Joao de Araujo · Apr 9, 2012 · Viewed 15.1k times · Source

I have some RDL reports created with SQL Server BI Development Studio and now I need to render them using the ASP.NET Report Viewer. Even though my RDLs contain references to the SQL server and the SELECT query, it keeps saying I need to specify a datasource for the report. Is there a way to make the datasource from the RDL be used or do I have to pass a datasource to the report viewer via C# code?

Thank you.

Answer

IvanH picture IvanH · Apr 17, 2012

I suppose you are using the Report Viewer in local mode:

viewer.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local

and you use viewer.LocalReport. In this case you has to run query yourself and pass the result to the viewer:

dim tbl as new DataTable()

Fill the data e.g. tbl.load(datareader).

Dim VDS As New ReportDataSource
VDS.Name = "Your Data Source Name"
VDS.Value = tbl
viewer.LocalReport.DataSources.Add(VDS)

If you want to use server mode

viewer.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Remote

You has to use viewer.ServerReport

viewer.ServerReport.ReportServerUrl = New Uri(ReportServerURL)

and probably viewer.ServerReport.SetDataSourceCredentials

Reporting Services and ReportViewer Controls in Visual Studio

Edit
How to get queries from rdl
Initialization:

Dim XRep As New XmlDocument
XRep.Load(ReportPath)
Dim xmlnsManager As New System.Xml.XmlNamespaceManager(XRep.NameTable)
dim DefaultNSURI as string = XRep.GetElementsByTagName("Width")(0).NamespaceURI
xmlnsManager.AddNamespace("rep", DefaultNSURI)

Dataset Processing:

For Each nd As XmlNode In XRep.SelectNodes("/rep:Report/rep:DataSets/rep:DataSet", xmlnsManager)
   'DataSourceName can be used to find iformation about connection' 
   Dim DataSourceName As String = nd.SelectSingleNode("rep:Query/rep:DataSourceName", xmlnsManager).InnerText  
   Dim DSName As String = nd.Attributes("Name").Value       
   cmd.CommandText = nd.SelectSingleNode("rep:Query/rep:CommandText", xmlnsManager).InnerText
   Dim tbl As New DataTable(DSName)
   tbl.Load(cmd.ExecuteReader)
    'The table created here is to be passed to  LocalReport as datasource'
 Next

Note To convert vb.net<->c# I use Convert VB.NET to C#