Search code examples
c#asp.net-mvccrystal-reports-2010

Show Multiple Crystal Reports in Loop


I have multiple Crystal Reports in checkboxlist, so that user can print/show multiple reports at same time.

Currently I am using session to pass the reportdocument, but most of the time the session value get replace before assigning to crystal report, as a result multiple reports contain same data. I have applied 3 sec delay on each loop but not reliable solution. And Also Image are not displaying in Reports.

Is there any elegant technique to do this??

Or

What will be alternative for Session variable?

Jquery:

$.each(chkBoxarr, function (index, value) {
 var w = window.open();
                $.ajax({
                    type: "POST",
                    url: "PrintReports",
                    traditional: true,
                    data: { id: value},
                    datatype: "json",
                    success: function (data) {
                        w.document.write(data);
                    },
                    error: function () {
                        alert("Error");
                    }
                });
});

Controller:

public ActionResult PrintReports(id)
{
  ReportDocument rpt = new ReportDocument();
  rpt.Load("~/ReportFileName.rpt");
  HttpContext.Session["rpt"] = rpt;
  return Redirect("~/Viewer.aspx");
}

Viewer.aspx.cs

Page_Init(object sender, EventArgs e)
{
    var rpt = System.Web.HttpContext.Current.Session["rpt"];
    CrystalReportViewer1.ReportSource = (ReportDocument)rpt;
}

Solution

  • Javascript:

    $.each(chkBoxarr, function (index, value) {
        $.ajax({
            url: "PrintReports",
            type: 'GET',
             data: { id: value},
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            success: function (data) {
                if (data.success) {
                    var URL = 'Viewer.aspx?type=' + data.URL;
                    window.open(URL);
                }
                else {
                    alert(data.message);
                }
            }
        });
    });
    

    Controller:

    public ActionResult PrintReports(id)
    {
        ReportDocument rpt = new ReportDocument();
        rpt.Load("~/ReportFileName.rpt");
        string guid = Guid.NewGuid().ToString();
        Session[guid] = rpt;
        return Json(new { success = true, URL = guid }, JsonRequestBehavior.AllowGet);
    }
    

    Viewer.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
        ReportName = Request.QueryString["type"].ToString();
        ReportDocument doc = new ReportDocument();
        doc = (ReportDocument)Session[ReportName];
        if (doc == null)
            Response.Write("<H2>Nothing Found; No Report name found</H2>");
        CrystalReportViewer1.ReportSource = doc;
    }
    

    Without sessions:

    $.each(chkBoxarr, function (index, value) {    
      var URL = 'Viewer.aspx?id=' + value;
      window.open(URL);            
    });
    
    protected void Page_Load(object sender, EventArgs e)
    {
       ReportDocument rpt = new ReportDocument();
       rpt.Load("~/ReportFileName.rpt");         
       CrystalReportViewer1.ReportSource = rpt;
    }
    

    For Displaying image add a aspx file CrystalImageHandler.aspx in folder where Viewer.aspx exist. also add

     <httpHandlers>
          <add verb="GET" path="CrystalImageHandler.aspx" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
    
        </httpHandlers>
    

    in Weconfig... Version number varies depends on your crystalreport version