Search code examples
c#-4.0sessioncrystal-reportspage-init

Crystal Report runs query multiple times on page change


I have written the code for crystal report in the page_init() and it runs fine, but I have just noticed that when I switch report page index, means when I want to load second page of the report, the control again goes to the page load and whole report query runs again.

I have made a switch case and from query string I take the name which report is to be loaded, is there a way by which when I can minimize the report load time?

Is session a better idea?

If I write the code in (!Ispostaback), then only first page is loaded, when I try to switch second page, it shows no resource is available error.

 protected void Page_Init(object sender, EventArgs e)
    {
        try
        {
            String test = Request.QueryString["ReportType"];
            ReportDocument report = new ReportDocument();
            switch (testCondition)
            {
                   case "Report":
                    var rpt1 = new LenderCompanyActionReport(); 
                    String lcarParam1 = Request.QueryString["Param1"];
                    String lcarParam2 = Request.QueryString["Param2"];
                    String lcarParam3 = Request.QueryString["Param3"];
                    String lcarParam4 = Request.QueryString["Param4"];
                    String lcarParam5 = Request.QueryString["Param5"];
                    List<Reportclass> lst2 = new ReportMethod().GetReport(lcarParam1, lcarParam2, lcarParam3, lcarParam4, lcarParam5);
                    rpt1.SetDataSource(lst2);
                    CRReportViewer.ReportSource = rpt1;
                    break;
}

Solution

  • You need to add rpt1 to a session variable