Skip to Content
0
Former Member
Mar 20, 2008 at 01:40 PM

How to pass parameters to the report containing subreports on runtime

25 Views

I am having a report which contains two subreports. I am passing the parameter to main report but not able to pass it to subreport. I want to pass the same parameters to the report containing subreports on runtime.

my code is below.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

using System.Configuration;

namespace Autofaktura.UserControls

{

public partial class ReportViewer : Autofaktura.UserControls.BaseUserControl

{

private long _orderId;

private Library.Common.Reports _report;

public ReportViewer(Autofaktura.AutofakturaMain.TabParameters tabParameters)

{

InitializeComponent();

_orderId = tabParameters.OrderId;

_report = tabParameters.Report;

}

private ReportDocument prepareReport(string reportName)

{

string databaseServer = Autofaktura.Library.Database.DataSource;

string databaseName = Autofaktura.Library.Database.InitialCatalog;

string databaseUsername = Autofaktura.Library.Database.UserID;

string databasePassword = Autofaktura.Library.Database.Password;

ReportDocument crReportDocument;

ConnectionInfo crConnectionInfo;

Database crDatabase;

Tables crTables;

TableLogOnInfo crTableLogOnInfo;

crReportDocument = new ReportDocument();

crReportDocument.Load(reportName, CrystalDecisions.Shared.OpenReportMethod.OpenReportByTempCopy);

crReportDocument.SetDatabaseLogon(databaseUsername, databasePassword, databaseServer, databaseName, false);

crDatabase = crReportDocument.Database;

crTables = crDatabase.Tables;

crConnectionInfo = new ConnectionInfo();

crConnectionInfo.AllowCustomConnection = true;

crConnectionInfo.ServerName = databaseServer;

crConnectionInfo.DatabaseName = databaseName;

crConnectionInfo.UserID = databaseUsername;

crConnectionInfo.Password = databasePassword;

foreach (CrystalDecisions.CrystalReports.Engine.Table aTable in crTables)

{

crTableLogOnInfo = aTable.LogOnInfo;

crTableLogOnInfo.ConnectionInfo = crConnectionInfo;

aTable.ApplyLogOnInfo(crTableLogOnInfo);

}

switch (_report)

{

case Library.Common.Reports.Bill:

crReportDocument.SetParameterValue("pOrderId", _orderId);

break;

case Library.Common.Reports.WorkOrder:

crReportDocument.SetParameterValue("pOrderId", _orderId);

break;

case Library.Common.Reports.Quotation:

crReportDocument.SetParameterValue("pOrderId", _orderId);

break;

default:

break;

}

return crReportDocument;

}

private void ReportViewer_Load(object sender, EventArgs e)

{

StringBuilder reportName = new StringBuilder();

reportName.Append(Application.StartupPath + "
Reports
");

switch(_report)

{

case Library.Common.Reports.Bill:

reportName.Append("Bill_right.rpt");

break;

case Library.Common.Reports.WorkOrder:

reportName.Append("WorkOrder.rpt");

break;

case Library.Common.Reports.Quotation:

reportName.Append("Quotation.rpt");

break;

default:

break;

}

crViewer.ReportSource = prepareReport(reportName.ToString());

}

private ParameterFields SetParameter(String fieldName, long fieldValue)

{

ParameterDiscreteValue parameterDiscreteValue = new ParameterDiscreteValue();

parameterDiscreteValue.Value = fieldValue;

return SetParameterFieldInfo(fieldName, parameterDiscreteValue);

}

private ParameterFields SetParameter(String fieldName, string fieldValue)

{

ParameterDiscreteValue parameterDiscreteValue = new ParameterDiscreteValue();

parameterDiscreteValue.Value = fieldValue;

return SetParameterFieldInfo(fieldName, parameterDiscreteValue);

}

private ParameterFields SetParameter(String fieldName, bool fieldValue)

{

ParameterDiscreteValue parameterDiscreteValue = new ParameterDiscreteValue();

parameterDiscreteValue.Value = fieldValue;

return SetParameterFieldInfo(fieldName, parameterDiscreteValue);

}

private ParameterFields SetParameter(String fieldName, DateTime fieldValue)

{

ParameterDiscreteValue parameterDiscreteValue = new ParameterDiscreteValue();

parameterDiscreteValue.Value = fieldValue;

return SetParameterFieldInfo(fieldName, parameterDiscreteValue);

}

private ParameterFields SetParameterFieldInfo(String fieldName, ParameterDiscreteValue fieldValue)

{

ParameterValues currentParameterValues = new ParameterValues();

currentParameterValues.Add(fieldValue);

ParameterField parameterField = new ParameterField();

parameterField.Name = fieldName;

parameterField.CurrentValues = currentParameterValues;

ParameterFields parameterFields = new ParameterFields();

parameterFields.Add(parameterField);

return parameterFields;

}

private void btnClose_Click(object sender, EventArgs e)

{

this.Close();

}

private void btnPrint_Click(object sender, EventArgs e)

{

crViewer.PrintReport();

Autofaktura.Library.Order.UpdateBillPrintedDate(_orderId);

}

}

}