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.
Did you verify the DataSourceReference element in your RDL? It needs the path to the reporting server.
The DataSourceReference element can contain a full folder path (for example, /SampleReports/AdventureWorks) or a relative path (for example, AdventureWorks). Relative paths start in the same folder as the report. The shared data source must be on the same server as the report.
Verify the DataSourceID too. Take a look at the answer on this question. It looks like it might be the same problem you are having.
If you are using an RDLC you could also set the datasource of your report manually using ReportDataSource. "GetMyData" in the example below would implement IEnumerable or IDataSource.
ReportDataSource reportDataSource = new ReportDataSource("MyDataName", GetMyData(startAt, endAt));
ReportViewer1.LocalReport.DataSources.Add(reportDataSource);
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reporting/MyReport.rdlc");
ReportParameterCollection col = new ReportParameterCollection();
ReportParameter startAtParam = new ReportParameter("StartAt", startAt.ToString("MMM, dd yyyy"));
col.Add(startAtParam);
ReportParameter endAtParam = new ReportParameter("EndAt", endAt.ToString("MMM, dd yyyy"));
col.Add(endAtParam);
ReportViewer1.LocalReport.SetParameters(col);
If you are converting an RDL to an RDLC you can follow the steps here. Note that you need to re-create data source and query information. Also, the XML schema definition is different between 2005 and 2008.
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#
来源:https://stackoverflow.com/questions/10073540/datasource-for-rdl-reports-with-reportviewer