How to change the query of a crystal report file (.rpt)? - c#

I need to change the query of my crystal report file (.rpt) depending of a filter (where clause). How can i do this (change the where clause of my query)? (In the code behind)
Obs: i'm using crystal report 13, .net framework 4.0 and c#.

Generally speaking, you can add conditions to the WHERE-clause by adding them to the report's record selection formula. For example, if you want records in the Order table that match, say, location 1 and last year then you can do something like:
string selectFormula = "{Order.LocationID} = 1 AND {Order.Year} = 2011";
Then add to your report definition via
crystalReportViewer.SelectionFormula = selectFormula;

Related

How to remove Database fields from Crystal report programmatically [duplicate]

I have a section in a Crystal Report that I want to suppress. I need to suppress it if there are 0 rows in a particular table in the dataset I am using. How would I do this? The Record Number special field provided appears to be an internal count of records in the report, and does not relate to the rows in the underlying data table.
I am creating the report from C#, but I cannot suppress the section from the code (it doesn't fit the project structure) - I must be able to do it from the report itself. The table concerned is definitely being passed to the report in the dataset, but it contains 0 rows. There must be a way to establish this inside the report itself.....
Can anyone please point me in the right direction?
In the Crystal Reports designer, view the properties of your section and there should be an option to Suppress, which you can give it a formula to return the appropriate boolean value.
You could then use the Count() function within that formula and (I believe) you can pass the name of your dataset to the Count() function to get the number of rows in that dataset.
I did the same thing on a complex report about 3 months ago but I don't have access to the report any more having changed jobs so I'm sorry I cannot be more specific, but hoepfully this gives you a starting point.
Just had a quick Google - try this.
If the section does only contain database fields and f.e. no text fields, then you could use the setting "Suppress Blank Section" in the "Section Export" (rightclick section) for that section.
As an alternative you could use the following formula in the "Suppress" in the "Section Export" for that section:
IsNull({table.field})
"{table.field}" is one of the fields in the dataset.
Hope this helps.
Go to "Section Expert" and click "Supress (No Drill-Down)" and try adding this:
IF {"DragYourFieldHere"} = "" then true else false
Create one dummy group,check on its header for each page,add the header in the group header of the dummy group.

Crystal Reports (C#) : Informations not displaying correctly

I'll separate this post in 3 sections to be as clear as possible. The first one will be informations you need to know. Second one will be the solutions I tried so far to get this working. Third section will be the questions. I also have to precise that's the first time I'm using CrystalReports.
Informations
I'm currently working with Visual Studio 2010 with C# and I'm using SAP Crystal reports. I'm displaying the data using MySQL database.
Now, here are the tables I use to display the report:
Table : orders
Fields : id(primaryKey), date(DATE), quantity(INT), fksizes(INT), fkclients(INT)
Table : sizes
Fields : id(primaryKey), name(VARCHAR(30)), price(FLOAT)
Relationship between these 2 tables : orders.fksizes with sizes.id
Basically, I'm passing a range of dates as parameters to the Crystal Reports to display informations only between those dates. Then, I created 2 parameters : StartDate and EndDate both of type Date.
What I've tried
Here's the original query I'm using to display what I want without the date range condition :
SELECT sizes.name, SUM(orders.quantity) AS totalQty,
(SUM(sizes.price) * orders.quantity) AS totalPrice,
orders.date
FROM orders
INNER JOIN sizes ON orders.fksizes = sizes.id
GROUP BY sizes.name, orders.date
This query works correctly and display the total quantity sold and the total price for each size name. At the report's footer, I'm using a Summary field in which I got the total sum of all totalQty named sumTotalQty. I have another field for the same purpose for totalPrice named sumTotalPrice.
I have 2 rows of data test which are :
Size name Quantity sold Total Price
------------------------------------------------------------------------------
Big 2 $6.00
XBig 7 $28.00
The field sumTotalQty displays 9 and sumTotalPrice displays $34.00 which is exact results. I have to precise that the first row has 2013-10-29 as value for orders.date and the second one 2013-10-30.
Now, I want to select a range of dates for which I want to display the results. As an example, I select from 2013-10-30 to today, I should get only the second row with sumTotalQty displaying 7 and sumTotalPrice displaying $28.00. I got the single row correctly displayed, but sumTotalQty displaying 9 and sumTotalPrice displaying $34.00 which are incorrect following the date range.
I then tried to add a WHERE clause to my sql query to specify the date range like this (in Database --> Database expert...):
SELECT sizes.name, SUM(orders.quantity) AS totalQty,
(SUM(sizes.price) * orders.quantity) AS totalPrice, orders.date
FROM orders
INNER JOIN sizes ON orders.fksizes = sizes.id
WHERE orders.date BETWEEN '{?StartDate}' AND '{?EndDate}'
GROUP BY sizes.name, orders.date
I got no result displayed with it. So, I think {?StartDate} and {?EndDate} are just not set but I'm really not sure. The same goes for WHERE orders.date BETWEEN #StartDate AND #EndDate.
Questions
Why aren't {?StartDate} and {?EndDate} set even if I have entered a date range when I'm prompted to give them ?
With the original query (without the WHERE clause), how can I get the right result in my summarize fields with the date range given ? It's like it sums from the database and not from the report fields.
EDIT
After many searches, 2 other questions came in my head :
Is it possible to add DateTimePicker in the report so the user will be able to enter a starting date and an end date easily ? The report would be refreshed automatically after that.
Is there a way to create or use Crystal Reports events (OnLoad, AfterLoad,...) ?
Thank you for all help I'll get.
Question 1: You either use BETWEEN (? AND ?) or BETWEEN (#StartDate AND #EndDate)
Question 2: You would have to group your data and then use totals in your report.
Edit: After discovering that the issue is with the parameter declaration, I suggest taking a look at some of the following posts:
http://www.dotnetperls.com/sqlparameter
Why do we always prefer using parameters in SQL statements?
Do some more research and you will find lots of info on the net. What I would suggest is to add some input boxes (TextBox, DateTimePicker, etc.) to your page where the user can input the parameter values and then pass them programmatically to the report. That way you can pass the parameters to the SQL statement and get the correct data to your report. You also won't need parameters in your report.
As far as CR events go, there is the Init event, which is public
http://msdn.microsoft.com/en-us/library/aa691447(v=vs.71).aspx
and then there are some Viewer events that could be useful
http://msdn.microsoft.com/en-us/library/ms227097(v=vs.80).aspx

How to design a Format with Multiple Column Report

I am designing a report With the Format With Multiple Columns option in the crystal report to display 10 records per column.But, my result set is Showing like,
1.Row1
2.Row2
3.Row3
4.Row4
5.Row5
6.Row6
7.Row7
8.Row8
9.Row9
10.Row10
11.Row11
12.Row12
13.Row13
14.Row14
15.Row15
16.Row16
17.Row17
18.Row18
19.Row19
20.Row20
instead of
1.Row1 11.Row11
2.Row2 12.Row12
3.Row3 13.Row13
4.Row4 14.Row14
5.Row5 15.Row15
6.Row6 16.Row16
7.Row7 17.Row17
8.Row8 18.Row18
9.Row9 19.Row19
10.Row10 20.Row20
what else I had to do to split the rows and display.
Note: I had already asked this kind of Question and got the answer to use Format With Multiple Column in this Link.
to set multiple columns you have first to do to Report -> Section Expert
Then in the details section you have to check Format with multiple columns
After that you should select the layout tab and set width of your column and the gab between them.

SSRS reports in multi culture language

I have MS Report server with a RDL file showing a nice report in English language. I have a web application with a report viewer to show this report within side a ASPX page.
The problem i have is I want to show same report in multiple languages?? How would I do it??
1) Store the localisation text in a database
2) Select the localised version of the text in a second dataset.
3) Set up a parameter for the user to select the language.
4) Use lookup functions to include the text in the report.
Useful if you want to apply the localisation system to many reports.
1) Add custom function to the custom code (Report Properties, Code) that accepts 'Section_Name' and 'Language' variables (e.g. "ReportName, French") and returns the localised text e.g.:
function Localise(Section as String) as string
if Report.Parameters!Language.Value = 1 then
select Section
case "Report_Name"
Localise = "Report Name in English"
case "Report_Description"
Localise = "Report Description in English"
end select
elseif Report.Parameters!Language.Value = 2 then
select Section
case "Report_Name"
Localise = "Report Name in French"
case "Report_Description"
Localise = "Report Description in French"
end select
end if
end function
2) Add a parameter for the user to select the language (in this example using integers as values)
3) Reference the code in the report textboxes as required, e.g.:
=code.localise("Report_Name")
This will be fairly quick to implement and maintain over a single report
Create one report per language and allow the user to choose the language by selecting a different report.
This will be quickest to implement but may make maintenance of the report difficult.

calculating sub-total in crystal report inserting the group section in c# windows appllication

I am using crystal reports in c#. I have calculated the sum of all the fields in my report using the running total fields from the field explorer for a column 'TotalCost'. Now i want to calculate the subtotal in the same report according to the column 'Type' for the same column 'TotalCost'. I have two Types in my report Assets and Liabilities. How can i calculate the subtotal for the TotalCost according to the Type using the running total field. Please can anyone give me step by step advise.
In the formula field (looks like little x+2) simply add the condition required for the running total to evaluate.
You'd enter it like this: {TableName.Type} = "Assets" or {TableName.Type} = "Liabilities".
If the condition isn't met then the formula tells the running total to ignore that record.

Categories