How to pass multiple parameters to Crystal Reports using C#? - c#

I used this code to pass a parameter to Crystal Reports, but I can't pass multiple parameters. How can I change this code to pass multiple parameters from C# to Crystal Reports?
this.Cursor = Cursors.WaitCursor;
RPT.RPT_Sale_by_day report = new RPT.RPT_Sale_by_day();
RPT.Form_RPT frm = new RPT.Form_RPT();
report.SetParameterValue("#date1", dt_num1.Value);
frm.crystalReportViewer1.ReportSource = report;
frm.ShowDialog();
this.Cursor = Cursors.Default;

//Initiating created Crystal Report
ParameterValues Params =new ParameterValues(); // Creating collection of parameters
ParameterDiscreteValue Par_Ref =new ParameterDiscreteValue(); // Discrete parameter that comes from SP and shows on Crystal Report
String PARAMETER1_VALUE ="123";
String PARAMETER2_VALUE = "ABC";
Params.Clear(); // Cleaning data collection
Par_Ref.Value = PARAMETER1_VALUE; // Assigning discrete value to our variable
Params.Add(Par_Ref); //Adding discrete parameter to parameter collection
report.DataDefinition.ParameterFields["PARAMETER1_NAME"].ApplyCurrentValues(Params); //Applying values from our collection to Crystal Report parameters
Params.Clear();
Par_Ref.Value = PARAMETER2_VALUE;
Params.Add(Par_Ref);
report.DataDefinition.ParameterFields["PARAMETER2_NAME"].ApplyCurrentValues(Params);

Related

How to set database name dynamically for a crystal report using C#?

I have a C# windows application in which there are many crystal reports.I call and show them using the following piece of code :
rptDDCollection rpt = new rptDDCollection();
rpt.SetDatabaseLogon(Connect.sDatabaseUserName, DLL.Connect.sDatabasePassword, "", Connect.sCurrentDatabase);
rpt.RecordSelectionFormula = "";
frmReports frm = new frmReports();
frm.crViewer1.DisplayGroupTree = false;
frm.crViewer1.ReportSource = rpt;
frm.crViewer1.SelectionFormula = rpt.RecordSelectionFormula;
frm.crViewer1.Show();
frm.Show();
Now, the issue is that I have 2 databases to work on.Things work fine with one database.I have to use the same report but very often I need to view data from the other database.The database engine is SQL server.While searching on the net and on stack overflow, I found suggestions to set the database name dynamically through code.What should be done to achieve what I want in this case?
I worked this around by setting database name for the tables in the report.I made one change in the actual code above.I created a method called ApplyLogonInfo and passed the report object to it.Inside the method I wrote the code for setting database name for the report tables dynamically.This is the modified code:
rptDDCollection rpt = new rptDDCollection();
rpt.SetDatabaseLogon(Connect.sDatabaseUserName, DLL.Connect.sDatabasePassword, "", Connect.sCurrentDatabase);
ApplyLogOnInfo(rpt);
rpt.RecordSelectionFormula = "";
frmReports frm = new frmReports();
frm.crViewer1.DisplayGroupTree = false;
frm.crViewer1.ReportSource = rpt;
frm.crViewer1.SelectionFormula = rpt.RecordSelectionFormula;
frm.crViewer1.Show();
frm.Show();
Below is the newly created method:
public static void ApplyLogOnInfo(ReportClass rpt)
{
TableLogOnInfo info = new TableLogOnInfo();
info.ConnectionInfo.DatabaseName = Connect.sCurrentDatabase;
for (int i = 0; i < rpt.Database.Tables.Count; i++)
{
rpt.Database.Tables[i].ApplyLogOnInfo(info);
}
}
sCurrentDatabase is the name of the database that has been currently selected for viewing.
This enabled me to set the database name dynamically, and now I can work with 2 (or in general multiple) databases and view the data from whichever database I wish to see.

How to pass a parameter to an SSRS report query?

I have created a report in my C# ASP.net web application, to generate a cover page of a proposal. I am attempting to pass the proposal ID to the report. My dataset is set up to accept the proposal ID as a query parameter, and I have the GetByProposalID and FillByProposalID methods defined.
The problem is that when the report runs, the report viewer does not contain data. Either the problem is in my code, or in my report / report viewer configuration.
Here's my method:
/// <summary>
/// Generate the cover page report as a PDF file from a given proposal
/// </summary>
/// <param name="ProposalID">Proposal ID from the database of the proposal</param>
public void GenerateCoverPage( int ProposalID )
{
ReportViewer viewer = new ReportViewer();
viewer.Reset();
viewer.ProcessingMode = ProcessingMode.Local;
viewer.LocalReport.ReportPath = "ReportCoverPage.rdlc"; //sets the report from the project RDLC file
//Connect the dataset to the report
ds_ReportCoverPage ds = new ds_ReportCoverPage();
ds_ReportCoverPage.dt_ReportCoverPageDataTable table = new ds_ReportCoverPage.dt_ReportCoverPageDataTable();
ds_ReportCoverPageTableAdapters.dt_ReportCoverPageTableAdapter ta = new ds_ReportCoverPageTableAdapters.dt_ReportCoverPageTableAdapter();
ta.FillByProposalID(table, ProposalID); //This SHOULD fill the adapter with the data from the selected proposal
ReportDataSource source = new ReportDataSource("ds_Report_ReportCoverPage", ds.Tables[0]); //Name must match the data source name within the report
viewer.LocalReport.DataSources.Add(source);
//Run-time exception that there is no report parameter "#Proposal"
//ReportParameter parm = new ReportParameter("#Proposal", ProposalID.ToString()); //Placeholder in report query
//viewer.LocalReport.SetParameters(parm);
viewer.LocalReport.Refresh();
string filepath = "C:\\Temp\\foo.pdf";
SavePDF(viewer, filepath);
}
As you can see, I tried to pass the parameter as a ReportParameter, but the parameter is in the query and not the report so it was rejected.
(I've gotten this far thanks to looking up other questions on SO.)
You will need a parameter in the report and then you can map that parameter to the parameter in the query in the data set.

How to Bind rdlc report using business object?

i have business object called "TeamMaster",
in which i define three properties,Id,Name & Flg.
in my .rdlc report i apply TeamMaster object as a data source,
now i write the following code in page load event of form in which i add report viewer control and i define my report as a local report.
using (RDLC_DEMO_DBEntities objdatabase = new RDLC_DEMO_DBEntities())
{
lstTeamMstr = objdatabase.TeamMasters.ToList();
}
this.TeamMasterBindingSource.DataSource = lstTeamMstr;
this.reportViewer1.RefreshReport();
when i check this code using debugging i get 6 records in TeamBindingSource,
but in windows report only displays six blank rows,
what is the problem?
Follow this code : >>
string path = HttpContext.Current.Server.MapPath(Your Report path);
ReportViewer1.Reset(); //important
ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Local;
// Add sub report even handler if you need
***ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(MySubreportProcessingEventHandler);***
LocalReport objReport = ReportViewer1.LocalReport;
objReport.ReportPath = path;
// Add Parameter If you need
List<ReportParameter> parameters = new List<ReportParameter>();
parameters.Add(new ReportParameter("Name", Value));
ReportViewer1.LocalReport.SetParameters(parameters);
ReportViewer1.ShowParameterPrompts = false;
ReportViewer1.ShowPromptAreaButton = false;
ReportViewer1.LocalReport.Refresh();
//Add Datasourdce
ReportDataSource reportDataSource = new ReportDataSource();
reportDataSource.Name = "Datasource Name Used due to report design";
reportDataSource.Value = DataSourceValue(Your object data-source);
objReport.DataSources.Add(reportDataSource);
objReport.Refresh();
Here Subreport Even handler code
private void MySubreportProcessingEventHandler(object sender, SubreportProcessingEventArgs e)
{
//You can get parameter from main report
int paramname = int.Parse(e.Parameters[0].Values[0].ToString());
//You can also add parameter in sub report if you need like main report
//Now add sub report data source
e.DataSources.Add(new ReportDataSource("DataSource Name",DataSourceValue)));
}
If you need to create Drillthrough report than follow this link Click here for Drillthrough report

Parameters not being passed into rdlc reports

I am trying to display rdlc reports in c# windows aplication.I am passing parameters to the report by this code-named
localReport.SetParameters(new ReportParameter[] { new ReportParameter("JobC", m) });
I have defined this parameter in the report also. But the parameter is not being passed into the report and the database table is not getting filtered. The first record, though, is being displayed., how to solve this error?
1.Go to the rdlc Design view
2.on the report data view (On the left) select parameters
3.Right click Add
4.set the name (best you Check Allow blank and allow null value)click OK
5.add the textfiled value OR select one from your tablix,right click and select Expression
6.set the expression e.g
=Parameters!title.Value
7.In your form that contains your report view add the following lines for a single parameter
ReportParameter rp = new ReportParameter("title", title); this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp });
});
reportViewer1.RefreshReport();
8.for multiple parameters
ReportParameter rp = new ReportParameter("title", title);
ReportParameter rps = new ReportParameter("expense", totalExpense.ToString());
this.reportViewer1.LocalReport.SetParameters(new R`enter code here`eportParameter[] { rp });
this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] { rps });
reportViewer1.RefreshReport();

Unable to display report in ASP.Net page

I have created a dataset and using it in my crystal report. Issue is that at design time i am able to display the data but when I execute it using ASP.Net page its showing a blank page. Below is my code that i am using to display the report programatically:
/// <summary>
/// Displays batch report
/// </summary>
/// <param name="agreementId"></param>
private void ShowStatements(string agreementId)
{
var crvStatements = new CrystalReportViewer();
var reportDocument = new ReportDocument();
reportDocument.Load(Server.MapPath("Statements.rpt"));
crvStatements.ReportSource = reportDocument;
var dt1 = new dsStatements.usp_GetBillDetailsByAgreementIdDataTable();
var dt2 = new dsStatements.usp_GetTransactionTypesByAgreementIdForBillDataTable();
var adapter1 = new usp_GetBillDetailsByAgreementIdTableAdapter();
var adapter2 = new usp_GetTransactionTypesByAgreementIdForBillTableAdapter();
adapter1.Fill(dt1, agreementId);
adapter2.Fill(dt2, agreementId);
//var statements = new dsStatements();
//statements.Tables.Add(dt1);
//statements.Tables.Add(dt2);
// reportDocument.SetDataSource(dsStatements);
crvStatements.RefreshReport();
}
Please note that i haven't used any control on the page. I am adding report viewer , report source and dataset programatically. Please help me out. I need it ASAP. I am using VS2010 with CR 2010
Can you provide the reason why you are creating the Crystal Report Viewer at runtime and not simply creating the control on the page? Some people do this so that they can export without displaying, but I don't see any code for that.
I haven't tested this, but I believe the below would work if you had a viewer control on your page.
private void ShowStatements(string agreementId)
{
//var crvStatements = new CrystalReportViewer(); //created on the page
ReportDocument reportDocument = new ReportDocument();
reportDocument.Load(Server.MapPath("Statements.rpt"));
crvStatements.ReportSource = reportDocument;
System.Data.DataTable dt1 = new dsStatements.usp_GetBillDetailsByAgreementIdDataTable();
System.Data.DataTable dt2 = new dsStatements.usp_GetTransactionTypesByAgreementIdForBillDataTable();
System.Data.DataSet statements = new System.Data.DataSet();
statements.Tables.Add(dt1);
statements.Tables.Add(dt2);
reportDocument.SetDataSource(dsStatements);
crvStatements.DataBind();
}
I'm assuming that the report is set up with these tables linked in the report, and you may have to give the datatables a name so that Crystal knows how to map each datatable to the corresponding table within the report. If possible you may try to limit the report to accepting a single table until you have the loading logic worked out.
If each of these datatables represent the data for a subreport then you'll have to iterate through those reports and set the datasources manually.
I hope this helps, but I answered this off the top of my head so there are probably things I've missed. If it doesn't work you just provide a comment and we can probably work through any issues.
The RefreshReport(); makes the Report appearing Empty. try it before loading the Datasets.

Categories