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);
}
}
}
|