I am using crystal report of VS 2008. I want to open a crystal report in PDF even when it is loaded with out showing the crystal report . I also not want to show the save dialog box while opening in PDF. With code below the report is opening directly in PDF but showing the save dialog box. How should i suppress the save dialog box and open the report directly in PDF?

CrystalReport1 oRpt = new CrystalReport1();
            
            DataSet ds = new DataSet();
            SqlDataAdapter adp0 = new SqlDataAdapter("SELECT * from test2", con);
            adp0.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                CrystalReportViewer1.SelectionFormula = "{test2.FirstName} = " + ds.Tables[0].Rows[0][0].ToString();
                CrystalReportViewer1.ReportSource = oRpt;
                
            }

            
            oRpt.Load(Server.MapPath("CrystalReport1.rpt"));
            ConnectionInfo connectionInfo = new ConnectionInfo();
            connectionInfo.DatabaseName = System.Configuration.ConfigurationSettings.AppSettings["DB"];
            connectionInfo.UserID = System.Configuration.ConfigurationSettings.AppSettings["UID"];
            connectionInfo.Password = System.Configuration.ConfigurationSettings.AppSettings["PWD"];
            connectionInfo.ServerName = System.Configuration.ConfigurationSettings.AppSettings["ServerName"];
            SetDBLogonForReport(connectionInfo, oRpt);
            System.IO.FileStream fs = null;
            long FileSize = 0;
            DiskFileDestinationOptions oDest = new DiskFileDestinationOptions();
            string ExportFileName = Server.MapPath("\\CrystalReport1.rpt") + "Export";
            oRpt.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
            oRpt.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
            oDest.DiskFileName = ExportFileName;
            oRpt.ExportOptions.DestinationOptions = oDest;
            oRpt.Export();
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("Content-Type", "application/pdf");
            Response.AddHeader("Content-Disposition", "attachment;filename=testing.pdf;");
            
            fs = new System.IO.FileStream(ExportFileName, FileMode.Open);
            FileSize = fs.Length;
            byte[] bBuffer = new byte[Convert.ToInt32(FileSize) + 1];
            fs.Read(bBuffer, 0, Convert.ToInt32(FileSize));
            fs.Close();
            Response.BinaryWrite(bBuffer);
            Response.Flush();
            Response.Close();

Please any one help me to figure it out.Thanks In advance
*Ranj*

Dani AI

Generated

Good catch, — the browser prompts come from sending the PDF as an "attachment". Safer and cleaner is to export the report into a memory stream and tell the browser you intend inline display. Also avoid writing the exported file to disk (less IO, fewer temp-file problems) and be sure to close/dispose the ReportDocument.

Example pattern (ASP.NET WebForms):

var rpt = new ReportDocument();
rpt.Load(Server.MapPath("~/Reports/CrystalReport1.rpt"));
// set DB logon / parameters here

using (var pdfStream = rpt.ExportToStream(ExportFormatType.PortableDocFormat))
{
    pdfStream.Position = 0;
    Response.Clear();
    Response.BufferOutput = true;
    Response.ContentType = "application/pdf";
    Response.AddHeader("Content-Disposition", "inline; filename=report.pdf");
    pdfStream.CopyTo(Response.OutputStream);
    Response.Flush();
    HttpContext.Current.ApplicationInstance.CompleteRequest();
}

rpt.Close();
rpt.Dispose();

Key troubleshooting notes:

  • Browser behavior is ultimately controlled by the client (PDF plugin/settings). You cannot guarantee inline view if the user configured their browser to always download PDFs.
  • Internet Explorer + HTTPS can force a Save dialog if the response has strict no-cache headers. If you need to serve over HTTPS, prefer using private caching: Response.Cache.SetCacheability(HttpCacheability.Private) so IE can render inline.
  • Do all report setup (Load, set DB logon, parameters or record selection) before exporting. Exporting before binding or logon often produces empty PDFs or errors.
  • Avoid Response.End (it throws ThreadAbortException). Use CompleteRequest or return a FileResult in MVC: return File(stream, "application/pdf").
  • Always Close/Dispose the ReportDocument to free native resources and avoid memory leaks.

If inline still fails, verify client browser/OS and test in Chrome/Firefox to confirm server-side headers are correct.

I solved the problem.No need of this line of code
Response.AddHeader("Content-Disposition", "attachment;filename=testing.pdf;");

Thanks if any one tried 4 this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.