Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
after much R&D also, I couldnot find solution to this. Can anybody help on the below issue addressed please? I'm facing performance issue while downloading PDF report using C#
C#
public class nachkPDF:System.Web.UI.Page
{
    private void Page_Load(object sender, System.EventArgs e)
    {
        ZurichNS.Ctl.Nachcalculation model = new ZurichNS.Ctl.Nachcalculation();
        String year = Request.Params["year"];
        long vstGC = -1;
        long mnr = -1;
        try
        {
            vstGC = long.Parse(Request["groupid"]);
        }
        catch
        {
            vstGC = -1;
        }
        try
        {
            mnr = long.Parse(Request["id"]);
        }
        catch
        {
            mnr = -1;
        }
        string path = model.createPDF(year, vstGC, mnr, Request, Session);
        Response.ClearHeaders();
        Response.ContentType="application/pdf";
        Response.AppendHeader("Connection","keep-alive");
        Response.AppendHeader("Content-Disposition"," attachment; filename = " + Session["user_id"] + "_" + year + "_nachk.pdf");
        long lngFileSize;
        byte[] bytBuffer;
        int iReading;
        string sFileName = path;
        System.IO.Stream outStream = Response.OutputStream;
        System.IO.FileStream fStream = new  System.IO.FileStream(sFileName,System.IO.FileMode.OpenOrCreate,System.IO.FileAccess.Read);
        lngFileSize = fStream.Length;
        bytBuffer = new byte[(int)lngFileSize];
        while((iReading=fStream.Read(bytBuffer,0,(int)lngFileSize)) > 0)
        {
            outStream.Write(bytBuffer,0,iReading);
        }
        fStream.Close();
        outStream.Close();
        Response.WriteFile(path);
        Response.End();
    }
    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
        //
        // CODEGEN: This call is required by the ASP.NET Web Form Designer.
        //
        InitializeComponent();
        base.OnInit(e);
    }

model.PDF internally calling 3 methods(mergeMaklersData,getAllMaklerdataNach,getOldYearMaklerdata)

ArrayList calcs = mData.mergeMaklersData((ArrayList)mData.getAllMaklerDataNach(y, oldYear.ToString(), vstGC, mnr),
(ArrayList)mData.getOldYearMaklerData(y, vstGC, mnr));
Posted
Updated 19-May-15 23:52pm
v2
Comments
Kornfeld Eliyahu Peter 20-May-15 5:52am    
Use your debugger and try to isolate the exact call that takes that much time...
Kornfeld Eliyahu Peter 20-May-15 6:04am    
Your code has several parts, like loading file, handling response and creating the PDF...
Isolate the exact part (probably to a single method call) that uses the most time of that 20 minutes...
priyanka arava 20-May-15 6:33am    
There are some foreach loops in mergeMaklerData which are taking more time.
Kornfeld Eliyahu Peter 20-May-15 6:36am    
Go into mergeMaklerData and try to isolate the exact lines that use the time up...
priyanka arava 20-May-15 9:23am    
you mean do i need to optimize the front end or stored procedures????

1 solution

Try this code:

protected void GetCTReport(string FSID)
    {
        try
        {
            ReportDocument rd = new ReportDocument();
            rd.Load(Page.Server.MapPath("CementingTreatmentReport.rpt"));
            rd.SetParameterValue("@FID", FSID);
            rd.SetDatabaseLogon("DB_9B08BD_automobile_admin", "webclient786");
            Print2Pdf(rd);

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

    }


protected void Print2Pdf(ReportDocument rd)
    {
        try
        {

            ReportDocument cryRpt = new ReportDocument();

            cryRpt = rd;

            ExportOptions CrExportOptions;

            DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();

            PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();

            string strDate = DateTime.Now.ToShortDateString();

            string Path = "~/ExportedReports/OptReport_NPS.pdf";

            if (Path != null || Path != string.Empty)
            {
                if (System.IO.File.Exists(Path))
                {
                    System.IO.File.Delete(Path);
                }
            }

            CrDiskFileDestinationOptions.DiskFileName = Server.MapPath(Path);

            CrExportOptions = cryRpt.ExportOptions;

            {

                CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;

                CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;

                CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;

                CrExportOptions.FormatOptions = CrFormatTypeOptions;

            }

            cryRpt.Export();
            Response.Redirect("SaveFile.ashx", false);//redirecting to GenericHandler
        }
        catch (Exception ex)
        {
            //throw;
            lblmsg.Visible = true;
            lblmsg.Text = ex.Message.ToString();
        }
    }


Create one Generic Handler:
public void ProcessRequest (HttpContext context)
{

        string Path = HttpContext.Current.Server.MapPath("~/ExportedReports/OptReport_NPS.pdf");
        byte[] bytesPDF = System.IO.File.ReadAllBytes(Path);

        if (bytesPDF != null)
        {
            System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
            response.ClearContent();
            response.Clear();
            string strDate = DateTime.Now.ToShortDateString();
            response.AddHeader("content-disposition", "attachment;filename= OptReport_" + strDate + ".pdf");
            response.ContentType = "application/octectstream";
            response.BinaryWrite(bytesPDF);
            response.End();
        }
}


Hope it would help you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900