Load RDL report into Web Report Viewer - asp.net

I am trying to load an (.rdl) report file into a web report viewer control (visual studio 2010 control):
//Get the data and
.
.
//Add it to report
ReportViewer.LocalReport.DataSources.Add(new ReportDataSource(element.Name, dt));
//Pass the report to the viewer
using (FileStream stream = new FileStream(ReportDocument.FileName, FileMode.Open))
{
this.ReportViewer.LocalReport.LoadReportDefinition(stream);
}
Am I missing a line of code somewhere? I used the equivalent for the winforms report viewer with the addition of RefreshReport() - however there is no equivalent method that I could find for web report viewers). The page remains blank - what am I missing?

There is a .Refresh() method, and that is what you are missing. Here's what I'm using (in VB):
ReportViewer1.Reset()
ReportViewer1.LocalReport.Dispose()
ReportViewer1.LocalReport.DataSources.Clear()
ReportViewer1.LocalReport.ReportPath = Server.MapPath("/reports/" & ReportFile)
ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource(<datasource>))
ReportViewer1.LocalReport.Refresh()

Related

Crystal Report direct saving as PDF, instead of viewing

I want to make a report from ASP.Net, in Crystal Report. I want, when user click on print, it should simply show a browser dialog of Save,Open,Save as, and PDF should be saved, or Crystal Report print preview should appear, I don't want to display report first in Viewer then click on button to get print or PDF, I want simply from clicking on asp button, I have all the idea of parameters and know how to make report, my question is just to not to show viewer and take report from asp button in a form of PDF or print preview dialog to print. I have used the Export method of .Net for Crystal Report, but it does not work.
You can generate a PDF by Using a Crystal Report and piece of code....
First: Generate a Crystal Report as per your requirements.
Second: Use the below code to generate the PDF:
Place the following name spaces at the top of the code page
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Variable Declaration
Dim CrReport As New CrystalReport1() // Report Name
Dim CrExportOptions As ExportOptions
Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions()
Dim CrFormatTypeOptions as New PdfRtfWordFormatOptions()
Set the destination path and file name
CrDiskFileDestinationOptions.DiskFileName = "c:\RichText.pdf"
Specify a page range (optional)
crFormatTypeOptions.FirstPageNumber = 1 // Start Page in the Report
crFormatTypeOptions.LastPageNumber = 3 // End Page in the Report
crFormatTypeOptions.UsePageRange = True
Set export options
CrExportOptions = crReport.ExportOptions
With CrExportOptions
// Set the destination to a disk file
.ExportDestinationType = ExportDestinationType.DiskFile
// Set the format to PDF
.ExportFormatType = ExportFormatType.PortableDocFormat
// Set the destination options to DiskFileDestinationOptions object
.DestinationOptions = CrDiskFileDestinationOptions
.FormatOptions = crFormatTypeOptions
End With
Trap any errors that occur on export
Try
// Export the report
CrReport.Export()
Catch err As Exception
MessageBox.Show(err.ToString())
End Try
Thats it.... Now you are ready to create a PDF of the Report.
Here is the solution you are looking for:
http://www.c-sharpcorner.com/UploadFile/mahesh/ExportCRtoPDF10062006161918PM/ExportCRtoPDF.aspx
Here is the quote from the site:
The following steps will guide you to achieve the same:
Add crystal report (.cr) file to your ASP.NET application.
Add a report instance on the page level.
Dim report As MyReport = New MyReport
Populate reports data on Page_Init
Dim ds As DataSet = GetData()
report.SetDataSource(ds)
Export Report
report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, False, "ExportedReport")
If you wish to format report to other formats, just change the ExportFormatType enumeration value to > your desired format.
If you wish to download the report, then you simply change the third parameter of >ExportToHttpResponse method in Step 4 to True.

Is there any way where i can using Crystal Report in ASP. NET MVC3 with Razor view engine [duplicate]

I know the use of server-side controls is a no-no in ASP.NET MVC, however we have a long list of crystal reports that the company has already produced for a previous application that I would like to utilize for our new ASP.NET MVC application.
Is there an appropriate way to use crystal reports in ASP.NET MVC? If so, how?
It is pretty simple actually. just add following references to your MVC project:
CrystalDecisions.CrystalReports.Engine
CrystalDecisions.ReportSource
CrystalDecisions.Shared
use Action method like below:
C# :
using CrystalDecisions.CrystalReports.Engine;
public ActionResult Report()
{
ReportClass rptH = new ReportClass();
rptH.FileName = Server.MapPath("[reportName].rpt");
rptH.Load();
rptH.SetDataSource([datatable]);
Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
return File(stream, "application/pdf");
}
VB.NET :
Imports CrystalDecisions.CrystalReports.Engine
Public Function Report() As ActionResult
Dim rptH As New ReportClass()
rptH.FileName = Server.MapPath("[reportName].rpt")
rptH.Load()
rptH.SetDataSource([datatable])
Dim stream As IO.Stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
Return File(stream, "application/pdf")
End Function
We had/have a similar situation at work.
The solution we use:
Create a seperate directory for reports
Create normal ASPX pages for reports
We have not seen any issues (besides the normal Crystal ones) with this setup.
Just add this reference : using CrystalDecisions.CrystalReports.Engine;
than do this action :
using CrystalDecisions.CrystalReports.Engine;
public ActionResult Report()
{
List<Table> table = new List<Table>();
ReportDocument rd = new ReportDocument();
rd.Load(Path.Combine(Server.MapPath("~/Repport/CrystalReport1.rpt")));
Response.Buffer = false;
Response.ClearContent();
Response.ClearHeaders();
Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "application/pdf", "Suivie Historique.pdf");
}

an error of report document object model

I created a report using report document object model in visual studio 2008 with vb.net. But I found one error. When the user clicks export button in client side, the following error will show. But the first time is OK before the user clicks export button.
Logon failed. Details: ADO Error Code: 0x Source: Microsoft OLE DB Provider for SQL Server Description: Login failed for user 'zanhtet'. SQL State: 42000 Native Error:
This is calling report code.
Dim ReportDocument As New ReportDocument()
Dim ReportPath As String = Server.MapPath("~/ReportDocumentOM/DBlogInRDOM.rpt")
ReportDocument.Load(ReportPath)
ReportViewer.ReportSource = ReportDocument
Dim ConnectionInfo As New ConnectionInfo
ConnectionInfo.ServerName = "ZANHTET\SQLEXPRESS"
ConnectionInfo.DatabaseName = "EAS_DevTrack4UDev"
ConnectionInfo.UserID = "zanhtet"
ConnectionInfo.Password = "123456"
For Each Table As Table In ReportDocument.Database.Tables
Dim TableLogOn As TableLogOnInfo = Table.LogOnInfo
TableLogOn.ConnectionInfo = ConnectionInfo
Table.ApplyLogOnInfo(TableLogOn)
Next
How can I solve this. Please, help me.
I am not sure your code above is invoked at what place. But if you are not already doing this, then handle important events from reportviewer. Inside those event handling method, make sure you invoke this authentication code again.
Export related event should do you a luck but you may have to handle couple of others as well (like for pagination also i had similar issues).
See here for Report Viewer Events
http://msdn.microsoft.com/en-us/library/microsoft.reporting.winforms.reportviewer.reportexport.aspx

Crystal Report print functionlity doesn't work after deployment?

I'm using crystal reports to build reports, everything is ok while development.
But after deployment of website, print functionality doesn't work.
I use _rptDocument.PrintToPrinter(1, false, 0, 0); to print report.
I've tried two methods to deploy website
Normal Publish option.
Web Deployment Project.
But I got the same output, print functionality doesn't work.
Also, I tried to set default printer, this also doesn't work.
Any ideas?
Printing on a web server isn`t a good idea. What should happen? A user prints on your servers printer? Use CR to create PDFs. Stream them to your clients. They can use their local printers.
try this :- Create PDF and open Browser Tab ...enter code here
string fname = "Report" + ".pdf";`enter code here`
//Create instance for crystal report Export option class
ExportOptions exprtopt = default(ExportOptions);
//create instance for destination option - This one is used to set path of your pdf file save
DiskFileDestinationOptions destiopt = new DiskFileDestinationOptions();
//Bind data in the crystal report first before export cystal report to PDF
ReportDocument RptDoc = new ReportDocument();
//Map your crystal report path
// RD.Load(Server.MapPath("~/CrystalReport2.rpt"));
//Set your crystal report datasource as dt
//Get path and assign into destination DiskFileName
destiopt.DiskFileName = Server.MapPath(fname);
exprtopt = RD.ExportOptions;
exprtopt.ExportDestinationType = ExportDestinationType.DiskFile;
//use PortableDocFormat for PDF data
exprtopt.ExportFormatType = ExportFormatType.PortableDocFormat;
exprtopt.DestinationOptions = destiopt;
//finally export your report document
RD.Export();
//To open your PDF after save it from crystal report
string Path = Server.MapPath(fname);
//create instance to client to open your pdf
WebClient client = new WebClient();
//Assign path to download pdf
Byte[] buffer = client.DownloadData(Path);
//metion content type as PDF and write
// Response.ContentType = "application/pdf";
//Response.AddHeader("content-length", buffer.Length.ToString());
//Response.BinaryWrite(buffer);
//======================================
Response.Write("<script>");
Response.Write("window.open('" + fname + "', '_newtab');");
Response.Write("</script>");
//===================================
IMTIYAZ

How to get started with Crystal Reports in ASP.NET and Visual Studio 2008?

How do I get the ASP.NET Controls for Crystal Reports in Visual Studio 2008? I've downloaded and installed a lot of things, but the guidance on the SAP website isn't very helpful to me.
What do I need to download and install on my machine to create an ASP.NET application which surfaces Crystal Reports?
I think Crystal Report is intergated with VS long time ago.Even there is a Reporting Tab that is shown in Toolbox.How to use it well,Simple Add Crystal Report file to your project from Add item option and follow the wizard.
How to bind it to your web site is also simple.Add Crystal report View control from toolbox to the your web form.Then do
protected void Page_Load(object sender, EventArgs e)
{
ReportDocument document = CreateReportDocument("MyReportFile");
CrystalReportViewer1.ReportSource = document;
}
public static ReportDocument CreateReportDocument(string crystalReportName)
{
ReportDocument crdocument = new ReportDocument(); // Report document variable
string reportfile = ConfigurationManager.AppSettings["CrystalReportFilePath"].ToString() + "\\" + crystalReportName + ".rpt";
try
{
DataTable report = new Datatable(); // You should put your data here
crdocument.Load(reportfile);// Load the report being displayed
crdocument.Database.Tables[0].SetDataSource(report);
}
catch (Exception exception)
{
throw exception;
}
return crdocument;
}

Resources