Run jasper reports from C#
Download
How to use
Download dependencies:
You will also need another library which comes with Web Services Enhancements 2 (WSE 2). You can download this add-on from here, or you can download only the needed component (Download dependencies) for the sake of simplicity.
First step is to add JasperDotNet.dll and Microsoft.Web.Services2.dll as references to your project.
We assume that you have a running instance of JasperReports Server on localhost, port 8080. If you are not so sure about it you can check by typing the following address in your browser's address bar:
localhost:8080/jasperserver/services/repository?wsdl
If you see a XML file containing Web Service Description Language definitions then everything is OK and you can proceed to the next step, which involves writing few lines of code:
using DotNetJasper;
namespace TestDotNetJasper
{
static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// create a JasperServer instance.
// change the JasperServiceUrl parameter according to your local setup
JasperServer js = new DotNetJasper.JasperServer("http://localhost:8080/jasperserver/services/repository");
// JasperServer does not (currently) have Integrated Windows Authentication
// so you must provide a user-name and a password
js.Username = "jasperadmin";
js.Password = "jasperadmin";
//create a simple report
JasperReport report = new JasperReport()
{
//setting this property is optional
Name = "Sample report",
//setting this property is mandatory. We'll use the path of one of the sample reports.
Path = "/reports/samples/AllAccounts",
//this report does not require any parameters to be passed
Parameters = null
};
string path = null;
try
{
// run the report that we created earlier.
// we specify that the output format is PDF and the output path is "C:\"
// the RunReport method will return the path to the generated file
path = js.RunReport(report, JasperReportFormat.PDF, @"C:\");
// open the file (if there is an associated program with the PDF format)
System.Diagnostics.Process.Start(path);
}
catch (Exception e)
{
//handle errors here
}
}
}
}
You can also use the asyncronous method RunReportAsync that will be more suitable for "big" reports.
As you will see, the RunReport/RunReportAsync method call can fail in some situations so the best practice is to enclose it in a try block.
...
Work in progress. Please come back later.
- 462 reads

Comments
Catsondbs (not verified)
Thu, 01/05/2012 - 09:37
Permalink
Reference for Jasper Server?
Hi, where could I find the reference for JasperServer in C#?
this is one of the very few places saying that C# could work with jasper report, please keep up the good work! thanks
Andrew
Thu, 01/05/2012 - 20:02
Permalink
RE: Reference for Jasper Server
Download DotNetJasper.dll from here and copy it somewhere under your project directory.
Then in your IDE, right-click your project name and click Add Reference...
In Add Reference window switch to Browse tab and select DotNetJasper.dll
You have to do the same with Microsoft.Web.Services2.dll. Notice that you can add multiple references at once with Shift/Control key. Good luck!
Add new comment