How to make multiple subreports? - c#

I try to get 2 subreports in my main report.
The subreports works separately but don't work together.
My main report:
I want it to look like this, but the lines are broken when it is too much.
Müşteri Hizmet Kayıtları
"Müşteri Hizmet Kayıtları" sub-report looks correct, but then comes to the front and end of "Müşteri Ödeme Kayıtları" sub-report.
It works without problems when the row number is small but it gets broken like this when the number of rows is large.
My codes:
DataTable dt = _datab.TableDataTable($"SELECT * FROM CariHizmetbyCari where CariId={_cari.CariId}");
DataTable dt2 = _datab.TableDataTable($"SELECT * FROM CariMuhasebebyCariHizmetId where CariId={_cari.CariId}");
DataSet dataSet = new DataSet();
dataSet.Tables.Add(dt);
dataSet.Tables.Add(dt2);
rptCariTotal rptCariTotal = new rptCariTotal();
rptCariTotal.Database.Tables[0].SetDataSource(dataSet.Tables[0]);
rptCariTotal.Database.Tables[1].SetDataSource(dataSet.Tables[1]);
rptCariTotal.SetParameterValue("pAdSoyad", _cari.CariAdı);
rptCariTotal.SetParameterValue("pMail", _cari.Mail);
rptCariTotal.SetParameterValue("pTelefon", _cari.Telefon);
rptCariTotal.SetParameterValue("pWebSite", _cari.WebSite);
crvViewer.ReportSource = rptCariTotal;
crvViewer.Refresh();

You can't have that first sub-report in Page Header.
Create two detail sections ( Right Click on Detail - Insert Section below) and set each sub report in each one.

Related

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.

Crystal report unlinked Subreport

I have a crystal report with several parts
Page Header a (Company Information)
Page Header b (Sub-Report: has list of specific countries)
Page Header c (Sub-Report: has list of specific Products)
Page Header d (Sub-Report: has list of specific items)
The data source of the report is given by a Dataset "DS1" through C# Application (Push Method)
Because of the complex logic in the background, it is decided to have Dataset "DS1" with 4 Data tables (tbl0,tbl1,tbl2,tbl3) to hold separately the correct data in the Dataset.
The dataset doesn't have any relationships, because I have loaded directly the final data result in the relative Data Tables in the Dataset
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
DataTable dt3 = new DataTable();
DataTable dt4 = new DataTable();
Load_CompanyInfo(country_ID, ref dt1);
Load_Countries(_ApprovalID, ref dt2);
Load_Products(_ApprovalID, ref dt3);
Load_Items(_ApprovalID, ref dt4);
_ds_tmp.Tables.Add(dt1);
_ds_tmp.Tables.Add(dt2);
_ds_tmp.Tables.Add(dt3);
_ds_tmp.Tables.Add(dt4);
In the Form ReportView.cs, I have assigned the Dataset to the report.
rep_c_single.SetDataSource(ds.Tables[0]); // Main company info
rep_c_single.Subreports[0].SetDataSource(ds.Tables[1]); // countries
rep_c_single.Subreports[1].SetDataSource(ds.Tables[2]); // products
rep_c_single.Subreports[2].SetDataSource(ds.Tables[3]); // items
rep_c_single.Refresh();
cr_Viewer.ReportSource = rep_c_single;
on the report file, the sub reports doesn't have link field to the main report.
Because final Data is prepared in the Data Tables.
I just want to display the Data in the Sub report independent from the Main Report data source.
What I have till now, the sub reports lists all countries and all products, not what in the data table exists.
EDIT:
It is important to mention: the sub reports and main reports don't have any relations. I want only to PUSH data to the relative Sub-reports
thanks for any help
i'm not entirely sure.. but i've done this once..
make sure your report has a group based on the primary key of the main report (with which you want to link all the sub reports)
right click on the sub report
change sub report links
in the available fields select the column name of the sub report or respective data table
click on add to add it into the fields to link
it'll also show in the sub-report paramater to use drop down list underneath
check the flag -- select data in sub-report based on field
select the main report primary key group you made in the first step
Hope it works.. :)

Fixed number of records in Crystal report

I have a SAP Crystal Report in which maximum records I get from database will be 12. The number of records can vary from 0 to 12 depends on the conditions. Now I need to display fixed 12 rows in my Crystal Report regardless of the number of records retrieved from database.
I suggest you to add new rows to your query results or to your DataTable, But as you want, I can suggest you a dirty way -That I don't test it yet-:
As you know you can have more than one Detail section by inserting new sections below it, and Crystal Report will name them like a, b and so on.
Now, You can add 11 detail sections after your current Detail section.
After Adding them you have your Detail section with data, and other detail sections that comes below it with an empty row, You can add a rectangle as a border and ... .
In properties of the section you can set a formula in front of Suppress by clicking on formula button, Like this formula -in Crystal Syntax-:
if count({your filed}) < 12 then false else true
Note :
12 is for the second detail section, that will reduced for other sections.
For myself in c# I use something like this (as easiest way):
DataTable dt = new DataTable();
dt = /* query result */;
for (int i = 12; i > dt.Rows.Count; i--)
DataRow dr = dt.NewRow();
If you have the database access.
Create a view with 12 dummy rows and outer join with your table. In this case you will get all 12 rows irrespective of number of rows in data table

Crystal Report multi table failure

I'm still new to C# and reports, and in order to take baby steps, I started with a Crystal Report using one table.
Eventually I figured it out and it worked brilliantly.
Then I added another table to the report. I haven't changed anything in my code. Adding a field from the second table to the report, results in a blank report.
Removing that field again (so no columns form the second table is on the report), the report produces data again.
So I get the impression that the problem is on the report side. But I have included the code anyway:
private void Load_Ord_Rep()
{
using (MySqlConnection conn = new MySqlConnection(OTW.Properties.Settings.Default.wcdbConnectionString))
{
conn.Open();
String sql = "SELECT * FROM wcdb.order_table, wcdb.mat_table WHERE order_no = '13661' and order_table.mat_code = mat_table.mat_code";
using (MySqlCommand cmdSel = new MySqlCommand(sql, conn))
{
DataSet ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter(cmdSel);
da.Fill(ds);
ReportDocument rpt = new ReportDocument();
rpt.Load("C:\\Visual Studio 2008\\Projects\\OTW\\OTW\\CrystalReport3.rpt");
dataView1.Table = ds.Tables[0];
rpt.SetDataSource(dataView1);
crystalReportViewer1.ReportSource = rpt;
crystalReportViewer1.Refresh();
}
conn.Close();
}
}
With further investigation I have come to the conclusion the problem is not the code or the link, but rather the loading of the second table. I did a outer join with the values being equal or greater. Only the first table's results are displayed on the report. So because the second table's values are not read, no join can be established between the two tables and thus no data on the report. Now the question: why is the second table not being read by Crystal Report!?
UPDATE
I removed the second table from the main report and added a sub-report with the data. Same result as before. The sup report shows blank. Running the sup report on its own (as the main report), it populated correctly. I'm using MySQL, could it then maybe be a database issue?
UPDATE
I created a new app, this time connected the report to the database using ODBC (instead of ADO.NET). And it worked perrrfect. Now to figure out why ADO.Net is not working....as my entire program is based on it.
You don't need to do any of the code you are doing with the dataset/dataview. I recommend you allow the report to "pull" data (you are "pushing" data to the report now).
To pull data requires only the following:
ReportDocument rpt = new ReportDocument();
rpt.Load("C:\\Visual Studio 2008\\Projects\\OTW\\OTW\\CrystalReport3.rpt");
rpt.SetDataBaseLogon("userName", "password", "servername", "database");
crystalReportViewer1.ReportSource = rpt;
This reduces the risk that you're confusing the report up by passing in a dataview that it has no idea how to map to the tables you added while designing the report. (Which I'm 99.999% sure is happening now.)
All the code above lacks is parameters if you had any that needed to be set. It looks like you may be trying to "push" data because you wanted to filter on that order number or something? If that is the case, add a 'record selection formula' inside your report, base it's 'where' aspect of that formula on a Crystal Report parameter and add a rpt.SetParameter(arguments) line to the code I provided.
No this is not a licensing constraint, I'm certain of that.
Successful troubleshooting path:
1) Used the simple code to load the report:
* No errors, but no data in the report
2) Played with join options in the report:
* Also resulted in a blank report, even with only one table
* Concerned the second table is not reading data
3) Checked for record filtering by a 'record selection formula' which might limit/prevent rows from being returned
* In this case, wasn't applicable
4) Analyzed how joins were being done:
["It's probably how you are joining. For example: if you do a right outer join to a second table and it has no data, then even the first table's rows won't come back."]
A join was done on the primary id field (mat_code) in the 'orders' table to the same field (mat_code) in the second table, 'material'. This seemed fine.
Attempted a Left Outer join with the link type being ">=". The report printed the first table (orders) with data. But still none of the second tables' (material) data.
Made a new report, this time first adding the "second" table (material) and then the first (orders). This time with an outer join, and only the material table shows data. In other words, it only appears to pull data for the first, or primary table, then stops.
5) Attempted loading the second table's data by putting it into a subreport, and linking the subreport on the same 'mat_code' field:
(Helpful tutorial: http://vb.net-informations.com/crystal-report/vb.net_crystal_report_subreport.htm)
Sub-report also shows blank. Only the main report works. Whatever table exists in the subreport is not being populated.
Still sees no data using the simple suggested code, his original code still shows the first table's data
6) Check for a mismatch between the table's connection and definition, and the actual data structure/contents being submitted to, or loaded by, the report. A specific test was suggested, go to:
i) 'Set Database Location' (where you manage tables for the report)
ii) Look to see if an XML/DataSet was used versus OLEDB was done
iii) Change the database table location to the same tables, but with an OLEDB connection type (repeat for all tables)
When attempting above, when the 'Update' button was hit, the screen just flickered for a split second, but showed no message. After testing the report again, still no change in behavior. He was using ADO.NET to connect to the data.
7) Still highly suspicious of the data table definition and connection type. Suggested the following test:
i) Make a brand new 1 page application with only simple test code.
ii) Make a new report with only the 'orders' and 'materials' tables, using OLEDB from the beginning
iii) Add only the mat_code field from the main table to the report
iv) Add a subreport for 'materials' linked on mat_code
v) Show only mat_code on the subreport
vi) Run the app
If data shows, the problem is either:
Answer 1: A mismatch between the database definition (as was read when the report was FIRST made, and connected via ADO.NET to the tables) and the actual data table/column definitions being found when loading the report later (i.e. Someone edited the 'materials' table to change a column definition, or the number of fields, etc.)
Answer 2: Possible defect in the particular combination of Crystal Reports and Windows drivers required to push ADO.NET data to the report. Using the pull-data model over OLEDB may be working around something issues. Those issues may be addressed with the latest Crystal Reports and/or Windows drivers (i.e. hotfixes or Windows updates, driver packs, etc.)
The test worked fine. Even without making a subreport. The data pulled in just like it was supposed to.
Check the link between the two tables in crystal reports. Are they as what you expect ?
Check it by
Field Explorer> DataBaseFields > (RightClick) DatabaseExpert > (See Tab) Links

Binding a DataSet to a MultiColumn MS Chart

I have a DataSet with multiple DataTables that I'd like to bind to a chart with multiple column series. I expected each series to have different values respective to the DataTables in the DataSets; however, when the chart appears, both series display the same values in the Y column, the values from the first tables.
Here is a general idea what my code is like:
(I created and filled up a DataSet named diffCharts, diffCharts contains tables table1 and table2, both tables have columns Month and Amount)
Chart1.Series.Add("table1");
Chart1.Series["table1"].XValueMember = "Month";
Chart1.Series["table1"].YValueMembers = "Amount";
Chart1.Series.Add("table2");
Chart1.Series["table2"].XValueMember = "Month";
Chart1.Series["table2"].YValueMembers = "Amount";
Chart1.DataSource = diffCharts;
Chart1.DataBind();
Am I missing something? I've never bound a DataSet with multiple tables to a chart before.... Does MS know how to handle this?
I know this is an old post, but better late than never.
You can bind enumerable data sources directly to the DataPointCollection property in the series using the DataBind method. This way each series can be bound to a different table.
I am relatively new to C#, so there may be an adverse affect to this method that I am not aware of. It functions correctly in my limited testing.
MSDN link.
Chart1.Series.Add("table1");
Chart1.Series["table1"].Points.DataBind(table.AsEnumerable(), "nameofXaxis", "nameofYaxis, "nameofPropertyaxis");
Propertyaxis can be left as an empty string
I don't think you can do that.
You'll probably need to create a table with one column as the shared column for the X Axis (Month) and then two more columns for the Y values you want to graph.
The DataTable class has a Merge function that may do exactly what you need.

Categories

Resources