Getting the relative path to the rdlc report in my winform app
Asked Answered
I

5

9

I am automatically creating a PDF from some of our reports in a month-end process. I am running into a problem where ReportViewer.LocalReport can't find my report. Within the project, the report files are in "(project root folder)/Reports/report.rdlc".

How do I set ReportViewer.LocalReport.ReportPath so I can reference my report file? I would rather not set the full path because I don't know where it would be installed when installed on the client machines.

Ides answered 23/4, 2010 at 21:28 Comment(0)
A
16

Use the Application.StartupPath property, it always points to directory where your EXE is located:

  using System.IO;
  ...

     string exeFolder = Application.StartupPath;
     string reportPath = Path.Combine(exeFolder, @"Reports\report.rdlc");

You'll want to make sure the report gets copied to your bin\Debug\Reports folder as well so it will work in the IDE. Use xcopy /s /d in a post-build event to get the file(s) copied.

Attainment answered 23/4, 2010 at 21:39 Comment(0)
I
0

The path is always relative to your "current directory" which in most cases is exe file- change rdlc files so that they are copied to the destination folder instead of embedded in resources(usually that's how this is done). The path in your project doesn't matter.

Illmannered answered 23/4, 2010 at 21:36 Comment(1)
This is intuitive, but in my experience not true. I've tried relative path and it just doesn't find the RDLC file.Monjan
P
0

You can store the reports in a "resources" file, and by using an Assembly reader of the given .dll / .exe where the "resource" file is embedded, can read as a streaming reference. Then set your report to the embeded stream to get the report definition.

Special note. If your report has nested sub-reports, the report will fail unless you also set in your LocalReport subreport definition another such reference to your subreport .rdlc file.

Pillowcase answered 23/4, 2010 at 21:37 Comment(0)
T
0

There's a bug from what I understand that is coming from Windows XP. The issue does not exhibits itself on Windows 7.

All the above answers stand, but I have a correction.

This issue exhibits itself once you use a SaveFileDialog which looks like changes the path of the current application. This happens when you use the current path, relative to current wokring directory which is the default for loading RDLC, ie reportpath = "nameOfReport.rldc". If you want to refresh the report after having used the SaveFileDialog the reportviewer cannot find the path to the report file.

The solution comes from Hans Passant on the post above but I would change from this

string exeFolder = Path.GetDirectoryName(Application.StartupPath);

to this

string exeFolder = Path.GetDirectoryName(Application.ExecutablePath);

Thanks Hans

Tandy answered 28/10, 2011 at 15:16 Comment(2)
can you give citation for the bug reference?Synergetic
@silent_warrior, this is not documented from what I know. I have tested this myself and have found that in all XP machines when I use the save file dialog in the same form as the report viewer, the dialog changes the path and a refresh to the report will fail.Tandy
P
0

Simple Solution is..

Default Path of project is projectname/bin/debug

in Report Viewer code Reportpath = "../../folder1/report1.rdlc"

The default path is projectname/bin/debug but if you create rdlc at root path for that you have to go at root but .net not provide such function so simply add ../ to change current directory to go perent directory.

i place ../../ two time bcz my rdlc at the root path so i have to change debug then bin and finally i reach the root path. if you have separate folder for rdlc then make sure that you that to write the foldername and place / our their.

Hope you like it..

Philodendron answered 23/5, 2019 at 7:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.