Ryan
Ryan

Reputation: 9928

Crystal Reports Viewer won't go past page 2

I have a Crystal Report Viewer control on an aspx page, which is supposed to have built-in paging.

When I click the "next page" button the first time, I move from page 1 to page 2, but every other time I click "next page" the report reloads to page 2.

Upvotes: 16

Views: 20542

Answers (10)

Paul Nicklin
Paul Nicklin

Reputation: 21

For me this worked fine until I upgraded from 13.0.1 to 13.0.10, so clearly something changed.

I've tried the above solution but it's complicated by the fact that we post back to update report parameters, and if you don't set the report parameters on the page load, the refresh fails. So As of today I haven't got it working without having something in the page load.

I suspect I might have to add some logic to check for changed param values and only refresh the report if one has changed.

In the end I deleted our "side bar" page up/down links and used the ones on the viewer. I could not see any way, given the page hooks available to make it work - Setting the report parameters resets the paging, but not setting them means the page won't load.

Upvotes: 0

Serghei T
Serghei T

Reputation: 77

I have some approach to share. I developed the simple CR wrapper that can help to render Crystal Reports. Use this code as is. Please feel free to modify, extend any part of the code. Download link https://1drv.ms/u/s!AlTLBrnU_bLUiaoUxcxNiRCZRQjWng

And the sample how to utilize the wrapper using CR wrapper class that enter code hereimplements IDisposable interface. Set report source in Page_Init and report parameters in Page_Load events and dispose report document in Page_Unload event.
The second method uses static class and render the report at ones, disposing report document at the end. The report source should be saved into session variable. Please be aware that second method is not really suitable in high traffic web application, because using of the static class. The collision will occur if two users would run any reports at the same time.

Method 1

using System;
using System.Linq;
using CrystalDecisions.Shared;
using System.Configuration;
using System.Web.Configuration;
using WebReports.Models.Helpers.CrystalReports; //Reference to CR wrapper


namespace YourNamespace
{
    public partial class ReportForm : System.Web.UI.Page
    {

        protected string _serverName;
        protected string _databaseName;
        protected string _schemaName;
        protected string _userId;
        protected string _userPassword;
        protected bool _integratedSecurity;
        protected string _databaseType;
        //Wrapper Report Document
        protected CReportDocument _reportDocument;

        /// <summary>
        /// Load report
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Init(object sender, EventArgs e)
        {
            //Wrapper object
            this._reportDocument = new CReportDocument();

            //These settings should be initialized from i.e. web.config in Page_PreInit

            this._reportDocument.ServerName = this._serverName;
            this._reportDocument.DatabaseName = String.Empty;
            this._reportDocument.SchemaName = this._schemaName;
            this._reportDocument.DatabaseType = CReportDatabaseType.ORACLE;
            this._reportDocument.UserId = this._userId;
            this._reportDocument.UserPassword = this._userPassword;
            this._reportDocument.IntegratedSecurity = false;


            //Get report name from query string. Define Your own method to get report name
            var parReportName = Request.QueryString["reportname"];

            if (String.IsNullOrEmpty(parReportName))
            {
                lblConfigError.Text = "Crystal Report name is not being provided.";
                return;
            }

            //Set Report file
            this._reportDocument.ReportFile = Server.MapPath("~/ReportFiles/") + parReportName;
            //Set Report documant
            this._reportDocument.SetReportDocument();
            //Get Report Document
            crViewer.ReportSource = this._reportDocument.GetReportDocument();

        }

        protected void Page_Load(object sender, EventArgs e)
        {

            CReportParameter reportParameter;

            //Get parameters Your own method to provide report parameters
            var parFimYear = RouteData.Values["par1"];
            var parFimCityCode = RouteData.Values["par2"];

            if (par1 == null || par2 == null)
            {
                lblConfigError.Text = "Crystal Report parameters are not being provided.";
                return;
            }

            //Define Report Parameter
            reportParameter = new CReportParameter();
            reportParameter.ParameterName = "@parYear";
            reportParameter.ParameterValue = parFimYear;
            reportParameter.crParameterValueKind = ParameterValueKind.StringParameter;
            _reportDocument.AddCReportParameter(reportParameter);

            reportParameter = new CReportParameter();
            reportParameter.ParameterName = "@parCityCode";
            reportParameter.ParameterValue = parFimCityCode;
            reportParameter.crParameterValueKind = ParameterValueKind.StringParameter;
            _reportDocument.AddCReportParameter(reportParameter);

            //Set report parameters
            this._reportDocument.SetReportParameters();

        }

        protected void Page_Unload(object sender, EventArgs e)
        {
            this._reportDocument.Dispose();
        }

    }
}

Method 2

using System;
using System.Linq;
using CrystalDecisions.Shared;
using System.Configuration;
using System.Web.Configuration;
using CrystalDecisions.CrystalReports.Engine;
using WebReports.Models.Helpers.CrystalReports; //Reference to CR wrapper

namespace YourNamespace
{
    public partial class ReportForm : System.Web.UI.Page
    {

        protected string _serverName;
        protected string _databaseName;
        protected string _schemaName;
        protected string _userId;
        protected string _userPassword;
        protected bool _integratedSecurity;
        protected string _databaseType;

        /// <summary>
        /// Load report
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Init(object sender, EventArgs e)
        {
            CReportParameter reportParameter;

            //These settings should be initialized from i.e. web.config in Page_PreInit

            if (!IsPostBack)
            {
                if (this._databaseType == CReportDatabaseType.ORACLE.ToString())
                {
                    //static wrapper class
                    CReportDocumentManager.ServerName = this._serverName;
                    CReportDocumentManager.DatabaseName = String.Empty;
                    CReportDocumentManager.SchemaName = this._schemaName;
                    CReportDocumentManager.DatabaseType = CReportDatabaseType.ORACLE;
                    CReportDocumentManager.UserId = this._userId;
                    CReportDocumentManager.UserPassword = this._userPassword;
                    CReportDocumentManager.IntegratedSecurity = false;

                    //Get report name from query string. Define Your own method to get report name
                    var parReportName = Request.QueryString["reportname"];

                    if (String.IsNullOrEmpty(parReportName))
                    {
                        lblConfigError.Text = "Crystal Report name is not being provided.";
                        return;
                    }
                        //get par1. Your own method to provide report parameters. This is from MVC application
                        var par1 = RouteData.Values["par1"];
                        //get par2
                        var par2 = RouteData.Values["par2"];

                        if (par1 == null || par2 == null)
                        {
                            lblConfigError.Text = "Crystal Report parameters are not being provided.";
                            return;
                        }

                        reportParameter = new CReportParameter();
                        reportParameter.ParameterName = "@parYear";
                        reportParameter.ParameterValue = par1;
                        reportParameter.CReportParameterValueKind = ParameterValueKind.StringParameter;
                        CReportDocumentManager.AddCReportParameter(reportParameter);

                        reportParameter = new CReportParameter();
                        reportParameter.ParameterName = "@parCityCode";
                        reportParameter.ParameterValue = par2;
                        reportParameter.CReportParameterValueKind = ParameterValueKind.StringParameter;
                        CReportDocumentManager.AddCReportParameter(reportParameter);



                    CReportDocumentManager.ReportFile = Server.MapPath("~/ReportFiles/") + parReportName;
                    ReportDocument doc = CReportDocumentManager.GetCReportDocument();
                    crViewer.ReportSource = doc;
                    Session[parReportName] = doc;

                }
            }
            else
            {
                var parReportName = Request.QueryString["reportname"];
                ReportDocument doc = (ReportDocument)Session[parReportName];
                crViewer.ReportSource = doc;

            }                
        }

    }
}

Upvotes: 0

Karthick Jayaraman
Karthick Jayaraman

Reputation: 301

It works for me!!

ReportDocument rd = new ReportDocument();

        protected void Page_Load(object sender, EventArgs e)
        {           
            string rptpath = WebConfigurationManager.AppSettings["ReportPath"].Replace("{TID}", Convert.ToString(Session["TID"]));
            rd.Load(rptpath);

            DataTable dtable = TemplateModel.LoadChequeData(Convert.ToString(Session["CID"]));
            rd.SetDataSource(dtable);           
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            CrystalReportViewer1.ReportSource = rd;
        }

Upvotes: 1

Khaled Eltabei
Khaled Eltabei

Reputation: 61

Put Report Source in Page_Init instead of Page_Load And report parameters Page_Load I think it works Probably

Upvotes: 1

Anh Nguyen
Anh Nguyen

Reputation: 1

I put all loading report in Page_Init instead of Page_Load. And It works properly.

Upvotes: 0

Spider man
Spider man

Reputation: 3330

Manually add the Page_Init() event and wire it up in the InitializeCompnent() with

this.Init += new System.EventHandler(this.Page_Init).

Move the contents of Page_Load to Page_Init().

Add if (!IsPostBack) condition in PageInIt.

protected void Page_Init(object sender, EventArgs e) {

    if (!IsPostBack)
    {
         ReportDocument crystalReportDocument = new ReportDocumment();
         crystalReportDocument.SetDataSource(DataTableHere);
         _reportViewer.ReportSource = crystalReportDocument;
         Session["ReportDocument"] = crystalReportDocument;
    }
    else
    {
          ReportDocument doc = (ReportDocument)Session["ReportDocument"];
          _reportViewer.ReportSource = doc;
    }
}

Upvotes: 5

Harshal
Harshal

Reputation: 1614

if (!Page.IsPostBack)
{
  //Write your Report display code 

  crystalRep.Load(Server.MapPath("DueFD.rpt"));
  crystalRep.SetDatabaseLogon(DB_UId, DB_Pass, Serv_Name, DB_Name);
  CrystalReportViewer1.ReportSource = crystalRep;

  // session code
  ReportDocument doc = (ReportDocument)Session["ReportDocument"];
  CrystalReportViewer1.ReportSource = doc;
}
else
{
  ReportDocument doc = (ReportDocument)Session["ReportDocument"];
  CrystalReportViewer1.ReportSource = doc;
}

Upvotes: 4

eternal
eternal

Reputation: 557

I had that problem too, check if there's not another WebControl interfering with Crystal (callback problem ? JavaScript ? I'm not sure). In my specific case, Dart File Upload and Crystal didn't get along well when put in the same page.

Upvotes: 0

lausite
lausite

Reputation: 1

I had that problem before on visual studio 2008, installing crystal reports basic service pack 1 solved the problem

Upvotes: 0

Ryan
Ryan

Reputation: 9928

The problem may stem from setting the Crystal Report Viewer control's ReportSource during the Page_Load event. This causes the paging information to be over-written with each page load, so the "current page" is re-set to 1 when it should be at 2.

As an easy solution, you can move the code that sets ReportSource into Page_Init

Upvotes: 24

Related Questions