locked
Save Crystal Report as PDF RRS feed

  • Question

  • User1119347574 posted

    Hi,

    I want to save crystal report as PDF when a user view that report..... so on my page load event i created the below code

    protected void Page_Load(object sender, EventArgs e)
    {

    string invoicenumber = "";
    invoicenumber = Request.QueryString["invoicenumber"];
    ReportDocument reportdocument = new ReportDocument();
    reportdocument.Load(Server.MapPath("CrystalReport.rpt"));
    reportdocument.SetDatabaseLogon("xxxxxx", "xxxxxxx", "xxxxxxxx", "xxxxxxx");
    reportdocument.SetParameterValue("@invoicenumber", invoicenumber);
    CrystalReportViewer1.ReportSource = reportdocument;
    DataSet dsrep = new DataSet();
    GetData(dsrep);
    reportdocument.SetDataSource(dsrep);
    reportdocument.ExportToDisk(ExportFormatType.PortableDocFormat, "C:\\report.pdf");
    }

    When executing the last line showing an error Missing parameter....any one help me urgent....

    Note: I used Stored Procedure for this...no XML file is created for this... with out creating XML file and Dataset manually i need to acheve this

    Friday, September 13, 2013 2:45 PM

All replies

  • User-1635195291 posted

    Hi sabarinathanitt87,

    May be permission issue when exporting to a folder. 
    You can try using temp folder as 

    string fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".pdf";

    then save to temp folder 

    reportDocument.ExportToDisk(ExportFormatType.PortableDocFormat, fileName);

    or else refer:

    Hope this helps.

    Thanks,

    Jatin

    Sunday, September 15, 2013 8:04 PM
  • User-37275327 posted

    Hope @ is not necessary.

    reportdocument.SetParameterValue("invoicenumber", invoicenumber);



    Sunday, September 15, 2013 10:30 PM
  • User1786833724 posted

    I had to do something quite similar.

    if you want to directly download the report without displaing it then

    //objReport.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Page.Response, true, "Test PDF.pdf");

    If you want to save to disk while the user is viewing the report on server's harddrive ( we had a feature that would save the reports that they've viewed and later user could download them on demand from their account :

    here is the stripped out version of the real code, as to cut of the clutter :

    protected void Page_Load(object sender, EventArgs e)
    		{
    			
    			DataTable tblOrderDetail = new DataTable("OrderDetail");
    			SqlCommand commnad = new SqlCommand("[dbo].[CustMultiOrdersDetail]");
    			commnad.CommandType = CommandType.StoredProcedure;
    			commnad.Parameters.Clear();
    			commnad.Parameters.Add(new SqlParameter("OrderID", "10643,10692,10702,10835,10952")); // just for demo
    			ReportHelper.GetQueryResultFromNorthwind(ref commnad, ref tblOrderDetail);
    			
    			CrystalReport1 objReport = new CrystalReport1();
    			ReportDocument doc = new ReportDocument();
    
    			objReport.SetDataSource(tblOrderDetail);
    			CrystalReportViewer1.ReportSource = objReport;
    			CrystalReportViewer1.RefreshReport();

                            //file name 'Test PDF.pdf' can be anything, you may also use GUID or something similar to get unique file name
                            //objReport.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Page.Response, true, "Test PDF.pdf"); objReport.ExportToDisk(ExportFormatType.PortableDocFormat, "C:\\Data\\Testy.pdf"); // file path is just for demo }

    Monday, September 16, 2013 1:05 AM
  • User1119347574 posted

    Hi, When i try your coding am getting error on below lines...

    What is ReportHelper and CrystalReport1

    ReportHelper.GetQueryResultFromNorthwind(ref commnad, ref tblOrderDetail); 
    CrystalReport1 objReport = new CrystalReport1();
    Tuesday, September 24, 2013 10:07 PM
  • User489113254 posted
    works for me 

    WebInventory.datasets.dsaccountreportTableAdapters.sp_reportinvetorymoveactaccountTableAdapter a = new WebInventory.datasets.dsaccountreportTableAdapters.sp_reportinvetorymoveactaccountTableAdapter(); Response.ClearContent(); Response.ClearHeaders(); ReportDocument rd = new ReportDocument(); rd.Load(Server.MapPath("~/reports/inventory/rptinventorymoveactaccount.rpt")); rd.SetDataSource(a.GetData().ToList()); rd.ExportToHttpResponse(ExportFormatType.PortableDocFormat,Response,true,"exportreport"); rd.Dispose();

    Monday, July 21, 2014 6:58 AM