10

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;
}
0

1 Answer 1

1
+50

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

2
  • Thanks for your suggestion; Is there any way without using sesssions? Commented Nov 4, 2015 at 3:13
  • if you have multiple page in your crystal report; the server side is triggered each time you click previous or next page. can you bit elaborate how to cop this with concurrent multiple reports without session (just add few description in your answer rather than just code; that would be very helpful) Commented Nov 4, 2015 at 4:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.