C# – How to generate Crystal Report in PDF format while passing Multiple Parameters

asp.netccrystal-reportspdf-generation

I want to generate crystal report in pdf format. I had done the same thing by passing one parameter. But this time I want to pass 10 parameter. I followed the same thing what I did for passing one parameter.

But now I got the Error Message "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack."
Anyone please give suggestions. Thanks in advance.
Please modify my Code according to generate Crystal Report in PDF format.

In the Button Click Event I have written the following code.

try
    {
        CrystalDecisions.CrystalReports.Engine.ReportDocument rpt =
                        new CrystalDecisions.CrystalReports.Engine.ReportDocument();

        string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        string[] str = conn.Split(';');
        string server = str[0].Substring(str[0].IndexOf(" = ") + 3);
        string database = str[1].Substring(str[1].IndexOf(" = ") + 3);
        string userid = str[2].Substring(str[2].IndexOf(" = ") + 3);
        string password = "Welc0me";

        rpt.Load(Server.MapPath("~/Reports/Marketing/JobOrdersList.rpt"));

        for (int i = 0; i < rpt.DataSourceConnections.Count; i++)
            rpt.DataSourceConnections[i].SetConnection(server, database, userid, password);
        rpt.SetParameterValue(0, DateTime.ParseExact(dcfromdate.DateString.ToString(), DateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None));
        rpt.SetParameterValue(1, DateTime.ParseExact(dcTodate.DateString.ToString(), DateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None));
        rpt.SetParameterValue(2, ddlCompany.SelectedValue);
        rpt.SetParameterValue(3, ddlUnit.SelectedValue);
        rpt.SetParameterValue(4, ddlCustomer.SelectedValue);
        rpt.SetParameterValue(5, ddlProduct.SelectedValue);
        rpt.SetParameterValue(6, ddlScope.SelectedValue);
        rpt.SetParameterValue(7, ddlStatus.SelectedValue);
        rpt.SetParameterValue(8, ddlGroupBy.SelectedValue);
        rpt.SetParameterValue(9, (ChkPrint.Checked == true ? "True" : "False"));
        rpt.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, HttpContext.Current.Response, true, "JobOrderList Report");

    }
    catch (Exception ex)
    {
        return ex.Message.ToString();
    }

Best Answer

//To generate PDF dynamically 

using System;
    using System.Windows.Forms;
    using CrystalDecisions.CrystalReports.Engine;
    using CrystalDecisions.Shared;

    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            ReportDocument cryRpt;

            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                cryRpt = new ReportDocument();
                cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt");
                crystalReportViewer1.ReportSource = cryRpt;
                crystalReportViewer1.Refresh(); 
            }

            private void button2_Click(object sender, EventArgs e)
            {
                try
                {
                    ExportOptions CrExportOptions ;
                    DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                    PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
                    CrDiskFileDestinationOptions.DiskFileName = "c:\\csharp.net-informations.pdf";
                    CrExportOptions = cryRpt.ExportOptions;
                    {
                        CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                        CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                        CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                        CrExportOptions.FormatOptions = CrFormatTypeOptions;
                    }
                    cryRpt.Export();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
    }
Related Topic