42

I have a report that is used by a windows service and a form application. So, I want to put embed the report in a DLL file that can be used by both.

The problem is that if I try to set the ReportEmbeddedResource property of a ReportViewer control in my windows form app, it will search the windows form app for the resource, not the dll file.

e.g.: Code from the windows form app:

rv.LocalReport.ReportEmbeddedResource = "MyReportInMyDLLFile.rdlc"

How can I make the above command look for the embedded resource in my DLL file?

JeffV
  • 52,985
  • 32
  • 103
  • 124
Jim
  • 11,229
  • 20
  • 79
  • 114

3 Answers3

57

Something like this should do it:

Assembly assembly = Assembly.LoadFrom("Reports.dll");
Stream stream = assembly.GetManifestResourceStream("Reports.MyReport.rdlc");
reportViewer.LocalReport.LoadReportDefinition(stream);
Greg Sansom
  • 20,442
  • 6
  • 58
  • 76
gschuager
  • 1,919
  • 1
  • 16
  • 25
  • 2
    When i use above code in `application page` in SharePoint 2010, following error raised:`Could not load file or assembly 'file:///c:\windows\system32\inetsrv\Reports.dll' or one of its dependencies. The system cannot find the file specified.`, but it works in WinForm Applications. – Amir Mar 17 '14 at 03:52
  • Such an old answer.. but works just as well as it does today! I followed the answers below.. where I have a report in another assembly within two folders, and it simply did not work, and yes it was an embedded resource. I copied and pasted it into the second line of this answer, and it worked! – sksallaj Jun 09 '15 at 17:57
  • can this method work when I am getting the reports from a signed `.exe` instead of a `dll`? – Smith Oct 14 '17 at 22:45
  • Worked great after I set LocalReport.ReportEmbeddedResource = null. Same with LocalReport.ReportPath. If not set to null when loading through a stream it will attempt to load the resource with either ReportEmbeddedResource or ReportPath. – David Glass Apr 16 '19 at 15:30
23

Just use the full namespace of the assembly, then folder names and then the name of the file:

rv.LocalReport.ReportEmbeddedResource = 
    "My.Assembly.Namespace.Folder1.Folder2.MyReport.rdlc";

Then make sure the report file is set as an embedded resource using the properties pane.

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
Dan Higham
  • 3,974
  • 16
  • 15
  • 1
    The folder1.folder2 part was critical. Thanks! – Jake Feb 12 '11 at 12:34
  • 5
    I'm not pretty sure, but I guess this only works if the report is in the same assembly where this code is. – Guillermo Gutiérrez Jun 26 '13 at 20:05
  • @GuillermoGutiérrez you are right: the report can read the embedded RDLC only in the same assembly, otherwise will throw an exception "There is no definitions with name MyNamespace.MyReport.rdlc" – T-moty Sep 26 '15 at 13:20
12

Probably the best thing to do would be to get a stream to the RDLC resource from the other assembly, then pass that to the "LoadReportDefinition" method of the Report Viewer control.

Details of how to get a stream from an embedded resource in a different assembly can be found here : Retrieving Resources with the ResourceManager Class

Additionally, you will need to refer to the embedded resource using it's full namespace path.

E.g. if you have an application with a default namespace of TheApp, and you keep a report called "MyReport.rdlc" in a folder called "Reports", the report reference call would be:-

rv.LocalReport.ReportEmbeddedResource = "TheApp.Reports.MyReport.rdlc";
DrCamel
  • 186
  • 5