Reading CrystalReport's Field Value Programmatically - c#

I'm using C#.NET to programmatically load a Crystal Report document (which gets its data from a database) and then export it as a PDF.
I'm stuck trying to read a fields (abc123) value from within the report (I want to use it in the filename). I don't mind where this field is in the crystal report; and it will not be repeated, but it needs to be a field in the report.
My code so far:
// Setup
ReportDocument reportTemplate = new ReportDocument();
reportTemplate.Load(textBox1.Text);
reportTemplate.Refresh();
reportTemplate.ReadRecords();
// Load the current reports name from the report summary information
String ReportName = reportTemplate.SummaryInfo.ReportTitle.ToString();
// Load the current reports "abc123" field from the report
// ????
// Export the report as PDF
reportTemplate.Export();
I've tried:
// Load the current reports "abc123" field from the report
String abcField = ((TextObject)rpt.Section2.ReportObjects["abc123"]).Text
String abcField = reportTemplate.ReportDefinition.ReportObjects["abc123"].ToString();
String abcField = reportTemplate.Rows.ReportDocument.ReportDefinition.ReportObjects["abc123"];
Without luck. Can anyone give some pointers? I can preview the report using crystalReportView1 object and the field is populated there.

Unfortunately you will not be able to pull a calculated field value from code behind. The calculated field value is not available until after the report engine takes over and does the rendering. You can access/modify formula definitions before the report is rendered but have no access to the actual value calculated from code behind. Even if you add a blank subreport and add a parameter to it from the main report for the field value you are trying to access it will not have a value before the reports engine renders it. You should definitely look for a way to pull it from the data you are binding the report with.

Related

Generate RDLC from XML String - Report is blank

I am facing an issue in the report in which my written xml is not bound into the report. The result is blank report.
Any help?
Links where i got the idea from:
https://gallery.technet.microsoft.com/scriptcenter/Dynamic-RDLC-Generator-6237aa44
Generating report from rdlc xml string
It Worked!, it was something related to parameters!!
parameters[0] = new ReportParameter("FeeColumnVisibility",
withFee.ToString());
System.Array.Resize(ref parameters, 4);
In which the Parameters are needed to declare whether i need some specific columns or not
The WithFee is a boolean variable that is also something related to my work

Add new fields using c # code to crystal report (rpt)

I have a crystal report rpt report created with some fixed fields that fill with a datatable, the fields obtained in the datatable may vary depending on the SQL statement of the fields in a form. Therefore, the question is whether in the design of the rpt form I can add, by code, new fields obtained in the datatable. For example, in the datatable I get the First and Last Name fields, and I show them in the report (in the design I have inserted the First and Last Name fields). Now in the datatable I get Name, Surname and Telephone, if I call the same report, it will fill in only Name and Surname, the Telephone field, not being inserted in the report will not appear, the idea is to add it. I am filling the dataset in such a way:
DataSet ds = new dsDataSet();
ds.Tables.Add(new DataTable());
foreach (DataColumn column in dtBusqueda.Columns)
{
ds.Tables[0].Columns.Add(column.ColumnName);
}
Now it would be those dataset fields to insert into the report as I explained before. The report is loaded as follows:
ReportDocument Report = new ReportDocument();
Report.Load("../../crReporte.rpt");
Report.SetDataSource(dtDataTable);
frmReportes form = new frmReportes(Report);
form.Show();
Thanks.
Crystal work only with pre-defined data object to display the data in a specified format. So if you pass a different data object then it will give you errors that it cannot locate your column.
I can offer :
1) Use same dataTable source for all (put tel column in original). So set visibility on Crystal Report for your special column. with supress.
2) You can create individual 2 different report And in your If Case you can load another report template if you want.
if it is only tel number not for complex requirement. use way 1. just set visibility.
But if you have complex requirement. I think you should use 2 different report template.

Pasing paramter values from a formula in Crystal Report

I have created an automated report system, that uses a formula to run a crystal report file.
When the report file has a SQL view linked to it, the formula sent is just the criteria that the report should show Ie "{PAY_TRANS_REPORT_DATA_VIEW.PAYDT} IN #startDT# to #endDT#"
However, I have some reports that use stored procedures which have parameters required.
My question, is how do I send these parameter values to the report?
Thanks.
Let the name of the report you want to pass a parameter is Report1 then you can pass your parameter, parameter1, to your report with the following way:
ReportClass.SetParameterValue("parameter1",parameter1);
The ReportClass is the class that's associated with the Report1.

Dynamically add rows of data to a *.rpt file (C#)

I am using vs2008 with CrystalReports and I'm wandering how can i dynamically add rows of data to a *.rpt file?(using c#).
In more detail i want to create a a function that populates a *.rpt file with data that might contain lists(for example "FirstName", "LastName", List<"Friend"> ;..Friend beeing an object that contains multiple fields like "FriendNr", "Address",....).
the code that i used so far is:
ReportDocument rpt = new ReportDocument();
MemoryStream stream = new MemoryStream();
string filename = filepath + "/myRpt.rpt";
rpt.Load(filename);
rpt.SetParameterValue(0, myObject.FirstName);
rpt.SetParameterValue(1, myObject.LastName);
Inside the rpt file i have placed FieldObjects(Parameter Fields), and i populate the file with data by assigning the desired values to these objects (" rpt.SetParameterValue(0, myObject.FirstName);" )
Please help me find a way to populate the report with the rows of data contained in the List also.
Thanks a lot for your time.
I don't think it is possible to add data rows to a report this way. I suggest using a Typed DataSet as your report data source. The report can then display as many Friend objects as you require.
Dynamic 'rows' approaches:
1). You could add more items to the parameter's CurrentValues collection. Not sure how you are using this in the report, but it may work for your purposes. Look at the ParameterFieldDefinition class for more information.
2). Create a DataSet, modify as necessary, then assign it to the report. Use the ReportDocument.SetDataSource() method to bind a report to data programmatically.
3). Another approach is to build a report that uses XML data, then programmatically modify the XML, then refresh the report.

How to create an unbound subreport in another unbound report

I have an unbound XtraReport that has a subreport control which contains another report. I call "unbound" to a report that has the definition of the fields using a schema but not actually bound to any DataSet, I create a DataTable using a Data Access Layer and then pass that object to the DataSource property of the report.
So, I have the following code:
// (...) Get the data from the db and fill a DataTable
if (table.Rows.Count > 0)
{
report.DataSource = table;
// (...) Get the data from the db and fill a DataTable for the subreport
report.SubPurchaseOrder.Report.DataSource = tableSubReport;
report.ShowPreviewDialog();
}
else
{
MessageBox.Show("No data to show.");
}
But what I get using this approach is the subreport printed very oddly (take a look at the attached pdf, sorry it's in spanish but I think you get the idea).
I've read the DevExpress documentation and maybe I'm not getting the approach right, so my question to you is how to create a report that has one or more subreports but I have to provide the data to fill them using some process external to the reports, such as a Data Access Layer?
Please let me know if the question is not stated correctly or lacks of more info.
EDIT:
I uploaded a sample project with the report with problem here.
I've tried to use parameters of some kind. In the BeforePrint event of the subreport control, I tried:
((XRSubreport)sender).ReportSource.FilterString = "[IdPO_RO] = " + _idPurchaseOrder;
and
((XRSubreport)sender).ReportSource.Parameters["Id"].Value = _idPurchaseOrder;
Of course, for the second, I added a parameter and the filter string the same as the first but using the parameter.
I could solve the problem.
The cause for this was that I was assigning to the wrong object. This line:
report.SubPurchaseOrder.Report.DataSource = tableSubReport;
should be:
report.SubPurchaseOrder.ReportSource.DataSource = tableSubReport;
So the brief explanation is that I was using another property to refer to the report contained in the subreport control (XRSubreport).

Categories

Resources