Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Security;
using System.Web.Configuration;

namespace testpro
{
public partial class Report2 : System.Web.UI.Page
{
        private ReportDocument report = new ReportDocument();
        SqlConnection cn = new SqlConnection(@"Password=*;Persist Security Info=True;User ID=sa;Initial Catalog=Production;Data Source=Server");
        SqlDataAdapter da;
        ProductionDataSet ds = new ProductionDataSet();
        ReportDocument cryRpt = new ReportDocument();

        protected void Page_Init(object sender, EventArgs e)
        {
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Username"] != null)
            {
                lblWelcome.Text = "Welcome" + "  " + Session["Username"].ToString();
            }

            if ((ViewState["ParametersShown"] != null) &&
                (ViewState["ParametersShown"].ToString() == "True"))
            {
                ShowReport();
            }
        }

        private void SetTableLocation(Tables tables)
        {
            ConnectionInfo connectionInfo = new ConnectionInfo();
            string server = WebConfigurationManager.AppSettings["ReportServer"];
            string user = WebConfigurationManager.AppSettings["ReportUserID"];
            string password = WebConfigurationManager.AppSettings["ReportUserPwd"];
            string database = WebConfigurationManager.AppSettings["ReportDatabase"];
            connectionInfo.ServerName = server;
            connectionInfo.DatabaseName = database;
            connectionInfo.UserID = user;
            connectionInfo.Password = password;

            foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
            {
                TableLogOnInfo tableLogOnInfo = table.LogOnInfo;
                tableLogOnInfo.ConnectionInfo = connectionInfo;
                table.ApplyLogOnInfo(tableLogOnInfo);
            }
        }

        protected void btnshow_Click(object sender, EventArgs e)
        {
            try
            {
                ShowReport();
                ViewState["ParametersShown"] = "True";
            }
            catch (Exception ex)
            {
            }
        }

        private void Page_Unload(object sender, System.EventArgs e)
        {
            if (report != null)
            {
                report.Close();
                report.Dispose();
                CrystalReportViewer1.ReportSource = null;
                CrystalReportViewer1.Dispose();
                CrystalReportViewer1 = null;
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
        }

        public void ShowReport()
        {
            //DateTime SDate = Convert.ToDateTime(TextBox1.Text);
            //DateTime EDate = Convert.ToDateTime(TextBox2.Text);
            //string ssDate = SDate.ToShortDateString();
            //string eeDate = EDate.ToShortDateString();
            string ssDate = TextBox1.Text;
            string eeDate = TextBox2.Text;

            string strLin;
            strLin = "";
            if (chkLine1.Checked == true)
            {
                strLin = strLin + "1";
            }
            if (chkLine2.Checked == true)
            {
                strLin = strLin + ",2";
            }
            if (chkLine3.Checked == true)
            {
                strLin = strLin + ",3";
            }
            if (chkLine4.Checked == true)
            {
                strLin = strLin + ",4";
            }
            if (chkLine5.Checked == true)
            {
                strLin = strLin + ",5";
            }
            if (chkLine6.Checked == true)
            {
                strLin = strLin + ",6";
            }
            string strshift;
            strshift = "'Z'";
            if (chkshift1.Checked == true)
            {
                strshift = strshift + ",'A'";
            }
            if (chkshift2.Checked == true)
            {
                strshift = strshift + ",'B'";
            }

            try
            {
                CrystalReportViewer1.Visible = true;
                CrystalReportViewer1.DisplayGroupTree = false;

                report.Load(Server.MapPath(@"~\CrystalReport6.rpt"));
                // report.SetDatabaseLogon("sa", "", "Server", "Production");
                string strSelection = "1=1 ";

                if (TextBox1.Text != null && TextBox1.Text != "" && TextBox2.Text != null && TextBox2.Text != "")
                {
                    report.SetParameterValue("startDate", TextBox1.Text);
                    report.SetParameterValue("endDate", TextBox2.Text);
                    //strSelection = strSelection + " AND {Command.Date}>='" + ssDate + "' AND {Command.Date}<='" + eeDate + "'";
                }
                if (strLin != "" && strLin != null)
                {
                    report.SetParameterValue("LineID", strLin);
                    // strSelection = strSelection + " AND {Command.LineID} in ("+strLin+")";
                }
                if (chkshift1.Checked == true)
                {
                    report.SetParameterValue("ShiftIDa", "A");
                }
                else { report.SetParameterValue("ShiftIDa", ""); }
                if (chkshift2.Checked == true)
                {
                    report.SetParameterValue("ShiftIDb", "B");
                }
                else
                {
                    report.SetParameterValue("ShiftIDb", "");
                }

                if (TextBox3.Text != "" && TextBox3.Text!= null)
                {
                    report.SetParameterValue("Sapno1", TextBox3.Text);
                   // strSelection = strSelection + " AND {Command.SapNo} in" + (TextBox3.Text) + " ";
                }

                report.RecordSelectionFormula = strSelection;
                SetTableLocation(report.Database.Tables);
                CrystalReportViewer1.ReportSource = report;
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void Btnlogout_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            FormsAuthentication.SignOut();
            Response.Redirect("Login.aspx");
        }
    }
}


What I have tried:

My report has 3 to 4 pages. It shows Page 1 and 2 in the viewer, but after navigating to page 3 it stops moving. If you change page while searching, like 3 of 3+, it goes back to page 1. After going to page 2 it stops going to page 1.

Pasting the Page_Load code in Page_Init is also not working fine for me. How can I resolve this?
Posted
Comments
Suvendu Shekhar Giri 1-Nov-16 3:05am    
any luck with debugging?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900