Wrox Programmer Forums
|
BOOK: Professional Crystal Reports for VS.NET
This is the forum to discuss the Wrox book Professional Crystal Reports for Visual Studio .NET by David McAmis; ISBN: 9780764544033
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Crystal Reports for VS.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 7th, 2006, 12:11 AM
Registered User
 
Join Date: Dec 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Crystal Report Sub Report Using C#

Hi All,

I have built an application using C#.net and Crystal Reports which are exported to PDF. The reports I have been using have been running very well. I generate the report from a dataset at runtime (see code below)
I have tried to modify some of my reports to include a sub-report which I have built an run separately as a report first.

I have included the sub report in the details section of the main report and specified the link field. I get errors when I try to run it.

I do not know how to write the C# code to get the sub report to run from the main report.
I have been unable to find any examples anywhere on the net or in books. It must be quite simple because these sub-reports are very common.

The code I use to generate the main report is below. I mostly understand this. Could anybody be so kind as to show me how to modify my code below to include the sub-report in the main report.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CrystalDecisions.Shared;

namespace ElecCon.Invoice.InvoiceItem1
{
    /// <summary>
    /// Summary description for InvoiceItemReport.
    /// </summary>
    public class InvoiceItemReport : System.Web.UI.Page
    {
        protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;
        string sSQLSelect;
        protected System.Data.SqlClient.SqlConnection sqlConnection1;
        protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
        protected ElecCon.Invoice.InvoiceItem1.DataSet77 dataSet771;
        protected System.Web.UI.WebControls.Button Button1;
        InvoiceItemDetailReport report = new InvoiceItemDetailReport();

        private void Page_Load(object sender, System.EventArgs e)
        {
            sSQLSelect = Session["ReportSelectionString"].ToString();
            SetUpReport();
            ExportTheReport();
        }

        private void SetUpReport()
        {
            try
            {
                DataSet dataSet771 = new DataSet();
                string sSQL = "SELECT invoice_Item_UID, job_Pack_UID, proj_Account_No,cpm_No, contract_UID, contract_Abbr, contract_Descr, invoice_UID, quote, extra,complete, "+
                    "invoice_Comment, invoice_Date, invoice_Work_OH, invoice_Month, invoice_Year, progress_UID, progress, project_Name, project_Description, feeder, "+
                    "equipment, old_Pole_No, item_Completed_Date, assy_No, assy_Descr, assy_Descr_Short, cost_Code, uom, quantity, work_Kind_Abbr, "+
                    "work_Kind_Descr, company, company_Abbr, contractor, class_Abbr, class_Description, customer_UID, customer_Abbr, customer_Name, "+
                    "notification_Date, Price, age,extra_Type, comment, resource_Cost "+
                    " FROM dbo.vwInvoiceItems "+ sSQLSelect + "";
                SqlConnection conn = new SqlConnection(ConnectDB.ConnectionString);
                SqlCommand sqlCommand = new SqlCommand(sSQL, conn);
                sqlCommand.CommandTimeout = 3600;
                SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
                da.Fill(dataSet771,"vwInvoiceItems");
                report.SetDataSource(dataSet771);

            }
            catch(Exception ex)
            {
                string x = Request.Url.AbsolutePath.ToString();
                Session["ErrorMessage"] = ex.ToString();
                Session["OriginatingUrl"] = x;
                Response.Redirect("..\\..\\ProcessError.aspx");
            }
            finally
            {

            }
        }

        #region Web Form Designer generated code

        }
        #endregion

        private void ExportTheReport()
        {
            ExportOptions exportOpts = new ExportOptions();
            DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions();
            string FName;
            FName = "C:\\Temp\\JobPackReport.pdf";
            diskOpts.DiskFileName = FName;
            exportOpts = report.ExportOptions;


            exportOpts.DestinationOptions = diskOpts;
            exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
            exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat;
            report.Export();

            // write the pdf file to the Client browser

            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            Response.WriteFile(FName);
            Response.Flush();
            Response.Close();

            // delete the exported file from the disk
            System.IO.File.Delete(FName);
        }

    }
}
 
Old April 6th, 2007, 11:28 PM
Registered User
 
Join Date: Apr 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi friend.
It may seem very late.
i have some suggested solution for your problem.
Make two datasets, one for the main report and the other for the subreprot.
populate both.
and then do as follows.

MyMainReport report = new MyMainReport();
report.SetDataSource(firstDataSet);
report.OpenSubReport(nameOfTheSubReport).SetDataSo urce(secondDataSet);

I've not tried it yet, but hope it will work.
good luck.

Right is might.
 
Old August 24th, 2008, 04:42 AM
Registered User
 
Join Date: Aug 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Though it is so late reply but sharing knowlwdge shouldn't be time dependent :)

Note:-
Assume that GetMainResult() and GetChangeState() return typed DataSet containng two tables "MainTableRecord" and "MainTableSubReportRecord" through which you have drop the field in your Reports.And also you bind subreport with the field which is common among MainTableRecord and MainTableSubReportRecord.

SOLUTION:
By this solution you will have a basic idea how to work with subReports in Crystal Report using C#.

ReportDocument customerReport = new ReportDocument();
customerReport.Load("D:\\MYWEBSITE\REPORTS\MainRep ort.rpt);

DataSet2 dsResult = rptMgr.GetMainResult();
DataSet2 dsSubReportResult = rptMgr.GetChangeState();

customerReport.SetDataSource(dsResult.Tables["MainTableRecord"]);

customerReport.OpenSubreport("MainSubReport.rpt"). SetDataSource(dsSubReportResult.Tables["MainTableSubReportRecord"]); ;

CrystalReportViewer1.Visible = true;
CrystalReportViewer1.ReportSource = customerReport;
CrystalReportViewer1.DataBind();
 
Old April 16th, 2009, 06:20 AM
hu_yang
Guest
 
Posts: n/a
Default C# crystal reports subreport

c# subreports in details

http://csharp.net-informations.com/c...subreports.htm

yang.
 
Old October 8th, 2009, 05:36 AM
Registered User
 
Join Date: Oct 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Post

ReportDocument customerReport = new ReportDocument();
customerReport.Load("D:\\MYWEBSITE\REPORTS\MainRep ort.rpt);

DataSet2 dsResult = rptMgr.GetMainResult();
DataSet2 dsSubReportResult = rptMgr.GetChangeState();

customerReport.SetDataSource(dsResult.Tables["MainTableRecord"]);

customerReport.OpenSubreport("MainSubReport.rpt"). SetDataSource(dsSubReportResult.Tables["MainTableSubReportRecord"]); ;

CrystalReportViewer1.Visible = true;
CrystalReportViewer1.ReportSource = customerReport;
CrystalReportViewer1.DataBind();
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote

These code work only for main report..main report display result..but sub report not show recods..pls help me.i want to display main report & sub report details.





Similar Threads
Thread Thread Starter Forum Replies Last Post
simple report and crystal report in vb.net saket123 .NET Framework 2.0 0 August 13th, 2008 06:55 AM
VS 2002 Crystal Report opens Seagate crystal 8 tusis_1 Crystal Reports 0 May 12th, 2007 11:45 AM
crystal report angelboy C# 2005 0 March 28th, 2007 12:50 PM
Crystal Report 8.5 reyboy Crystal Reports 3 January 15th, 2005 03:40 AM
Error While printing report (Crystal report) vikaspaweb Pro VB 6 0 March 8th, 2004 09:53 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.