locked
crstal report RRS feed

  • Question

  • User1594475756 posted

    Hi,

       I want to generate crystal report throw c# code.That contains the data .the data will be loaded from database .

    Please help me

    Friday, August 12, 2011 8:42 AM

Answers

  • User546554901 posted

    You can try this.In Calling form,Where you want to pass parameter for the page.

     protected void btnPrint_Click(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                ReportDocument oDoc = new ReportDocument();
                DateTime FromDate = default(DateTime);
                DateTime ToDate = default(DateTime);
                int PartyTypeId = default(int);
                int PartyId = default(int);

                PartyTypeId = ddlType.SelectedIndex > 0 ? Convert.ToInt32(ddlType.SelectedValue) : 0;
                PartyId = ddlName.SelectedIndex > 0 ? Convert.ToInt32(ddlName.SelectedValue) : 0;

                List<AccGeneralLedger> lstAccGeneralLedger = new List<AccGeneralLedger>();

                FromDate = !string.IsNullOrEmpty(txtFormDate.Text) ? Convert.ToDateTime(txtFormDate.Text, CultureInfo_BD()) : Convert.ToDateTime(ContextConstant.MINIMUM_DATE);
                ToDate = !string.IsNullOrEmpty(txtToDate.Text) ? Convert.ToDateTime(txtToDate.Text, CultureInfo_BD()) : Convert.ToDateTime(ContextConstant.MINIMUM_DATE);

                oDoc.Load(Request.MapPath(Request.ApplicationPath) + "/Reports/Accounts/Crystal/AccBillBook.rpt");

                dt = new AccBillBookReportBLL().Report_AccBillBook(FromDate, ToDate, PartyTypeId, PartyId);
                oDoc.SetDataSource(dt);
                
                oDoc.SetParameterValue("@StartDate", txtFormDate.Text);
                oDoc.SetParameterValue("@EndDate", txtToDate.Text);
              
               Session["ReportDoc"] = oDoc;

                String url = "~/Reports/Accounts/ReportView.aspx";
              
                Helper.Redirect(url, "_blank", "location=1,status=1,scrollbars=1,width=1000,height=1000");
            }

    In ReportView.aspx.cs

    private void ShowReport()
        {
            Helper.SetDataBaseLogonForReport(((ReportDocument)Session["ReportDoc"]));
            CrystalReportViewer1.ReportSource = ((ReportDocument)Session["ReportDoc"]);      
            CrystalReportViewer1.DisplayGroupTree = false;
            CrystalReportViewer1.DataBind();
            
        }

     protected void CrystalReportViewer1_Init(object sender, EventArgs e)
        {
            ShowReport();
        }

    In Helper Class:

      public static void SetDataBaseLogonForReport(ReportDocument oReportDocument)
        {
            System.Data.Common.DbConnection connection = (DbProviderHelper.GetConnection());
            String connStr = connection.ConnectionString;

            string[] connValues = connStr.Split(';');
            string[] serverName = connValues[0].Split('=');
            string[] dbName = connValues[1].Split('=');
            string[] userID = connValues[2].Split('=');
            string[] password = connValues[3].Split('=');
            System.Data.SqlClient.SqlConnectionStringBuilder SConn = new System.Data.SqlClient.SqlConnectionStringBuilder(connection.ConnectionString);
            //oReportDocument.SetDatabaseLogon(SConn.UserID, SConn.Password, SConn.DataSource, SConn.InitialCatalog);
            oReportDocument.SetDatabaseLogon(userID[1], password[1], serverName[1], dbName[1]);

        }

    public static void Redirect(string url, string target, string windowFeatures)
        {
            HttpContext context = HttpContext.Current;
            if ((String.IsNullOrEmpty(target) ||
                target.Equals("_self", StringComparison.OrdinalIgnoreCase)) &&
                String.IsNullOrEmpty(windowFeatures))
            {
                context.Response.Redirect(url);
            }
            else
            {
                Page page = (Page)context.Handler;
                if (page == null)
                {
                    throw new InvalidOperationException(
                        "Cannot redirect to new window outside Page context.");
                }

                url = page.ResolveClientUrl(url);
                string script;
                if (!String.IsNullOrEmpty(windowFeatures))
                {
                    script = @"window.open(""{0}"", ""{1}"", ""{2}"");";
                }
                else
                {
                    script = @"window.open(""{0}"", ""{1}"");";
                }
                script = String.Format(script, url, target, windowFeatures);
                ScriptManager.RegisterStartupScript(page,
                    typeof(Page),
                    "Redirect",
                    script,
                    true);
            }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 12, 2011 9:41 AM
  • User-1974373719 posted

    check out this link may help you out http://devtoolshed.com/content/crystal-reports-and-aspnet

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 12, 2011 9:43 AM
  • User-1974373719 posted

    http://www.codeproject.com/KB/aspnet/Crstalreportusingvs2010.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 12, 2011 9:43 AM

All replies

  • User546554901 posted

    You can try this.In Calling form,Where you want to pass parameter for the page.

     protected void btnPrint_Click(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                ReportDocument oDoc = new ReportDocument();
                DateTime FromDate = default(DateTime);
                DateTime ToDate = default(DateTime);
                int PartyTypeId = default(int);
                int PartyId = default(int);

                PartyTypeId = ddlType.SelectedIndex > 0 ? Convert.ToInt32(ddlType.SelectedValue) : 0;
                PartyId = ddlName.SelectedIndex > 0 ? Convert.ToInt32(ddlName.SelectedValue) : 0;

                List<AccGeneralLedger> lstAccGeneralLedger = new List<AccGeneralLedger>();

                FromDate = !string.IsNullOrEmpty(txtFormDate.Text) ? Convert.ToDateTime(txtFormDate.Text, CultureInfo_BD()) : Convert.ToDateTime(ContextConstant.MINIMUM_DATE);
                ToDate = !string.IsNullOrEmpty(txtToDate.Text) ? Convert.ToDateTime(txtToDate.Text, CultureInfo_BD()) : Convert.ToDateTime(ContextConstant.MINIMUM_DATE);

                oDoc.Load(Request.MapPath(Request.ApplicationPath) + "/Reports/Accounts/Crystal/AccBillBook.rpt");

                dt = new AccBillBookReportBLL().Report_AccBillBook(FromDate, ToDate, PartyTypeId, PartyId);
                oDoc.SetDataSource(dt);
                
                oDoc.SetParameterValue("@StartDate", txtFormDate.Text);
                oDoc.SetParameterValue("@EndDate", txtToDate.Text);
              
               Session["ReportDoc"] = oDoc;

                String url = "~/Reports/Accounts/ReportView.aspx";
              
                Helper.Redirect(url, "_blank", "location=1,status=1,scrollbars=1,width=1000,height=1000");
            }

    In ReportView.aspx.cs

    private void ShowReport()
        {
            Helper.SetDataBaseLogonForReport(((ReportDocument)Session["ReportDoc"]));
            CrystalReportViewer1.ReportSource = ((ReportDocument)Session["ReportDoc"]);      
            CrystalReportViewer1.DisplayGroupTree = false;
            CrystalReportViewer1.DataBind();
            
        }

     protected void CrystalReportViewer1_Init(object sender, EventArgs e)
        {
            ShowReport();
        }

    In Helper Class:

      public static void SetDataBaseLogonForReport(ReportDocument oReportDocument)
        {
            System.Data.Common.DbConnection connection = (DbProviderHelper.GetConnection());
            String connStr = connection.ConnectionString;

            string[] connValues = connStr.Split(';');
            string[] serverName = connValues[0].Split('=');
            string[] dbName = connValues[1].Split('=');
            string[] userID = connValues[2].Split('=');
            string[] password = connValues[3].Split('=');
            System.Data.SqlClient.SqlConnectionStringBuilder SConn = new System.Data.SqlClient.SqlConnectionStringBuilder(connection.ConnectionString);
            //oReportDocument.SetDatabaseLogon(SConn.UserID, SConn.Password, SConn.DataSource, SConn.InitialCatalog);
            oReportDocument.SetDatabaseLogon(userID[1], password[1], serverName[1], dbName[1]);

        }

    public static void Redirect(string url, string target, string windowFeatures)
        {
            HttpContext context = HttpContext.Current;
            if ((String.IsNullOrEmpty(target) ||
                target.Equals("_self", StringComparison.OrdinalIgnoreCase)) &&
                String.IsNullOrEmpty(windowFeatures))
            {
                context.Response.Redirect(url);
            }
            else
            {
                Page page = (Page)context.Handler;
                if (page == null)
                {
                    throw new InvalidOperationException(
                        "Cannot redirect to new window outside Page context.");
                }

                url = page.ResolveClientUrl(url);
                string script;
                if (!String.IsNullOrEmpty(windowFeatures))
                {
                    script = @"window.open(""{0}"", ""{1}"", ""{2}"");";
                }
                else
                {
                    script = @"window.open(""{0}"", ""{1}"");";
                }
                script = String.Format(script, url, target, windowFeatures);
                ScriptManager.RegisterStartupScript(page,
                    typeof(Page),
                    "Redirect",
                    script,
                    true);
            }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 12, 2011 9:41 AM
  • User-1974373719 posted

    check out this link may help you out http://devtoolshed.com/content/crystal-reports-and-aspnet

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 12, 2011 9:43 AM
  • User-1974373719 posted

    http://www.codeproject.com/KB/aspnet/Crstalreportusingvs2010.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 12, 2011 9:43 AM