Archive for the ‘Crystal Report’ Category

Apply Formatting in formula in crystal report

If you want to apply condition base formatting in crystal report, you can use below method for that.

if (IsNumeric({Student.Marks})) Then
if({Student.Marks} >= 40) then
crbold   //font bold – PASS
else
crregular //font regular – FAIL
else
crregular //if non-numeric/empty/NULL make the font regular – FAIL
=============================================================
To change the font style dynamically according to value, use this expression in your expression

=Format(First(Fields!Field_value.Value, “ABCDEFG”),”BOLD”)

Advertisements

Remove the decimal places in Crystal Report

Solution

The easiest way to fix the output is to use the ‘ToText’ function. This allows you to apply formatting – most importantly, remove the decimal places.

totext({Field_Name},0,””)

DateAdd Function in Crystal Report

 

The DateAdd() function can be used to add an interval of time to a date.

Syntax:

DateAdd (intervalType, number, startDate)

Interval Type Description
yyyy Year
q Quarter
m Month
y Day of year (same result as w or d.)
d Day (same result as w or y.)
w Weekday (same result as y or d.)
ww Weeks
Adds the indicated number of weeks to startDate.
h Hours
n Minutes
s Seconds

 

Example :

DateAdd(“D”, 1, “15/04/2014”)         ==>     Output : 16/04/2014

DateAdd(“M”, 1, “15/04/2014”)         ==>     Output : 15/05/2014

Set custom paper size in crystal report on web server in asp.net c#

Problem :

When developer set custom paper size in crystal report on development machine, it will  work. But when developer deploys project setup on web server, report generated by web system will be in default paper size of printer located on web server. So How to bind custom paper size with crystal report on web server.

What exactly happening during report generation

Crystal Reports picks up the paper size from the printer that developer has the report bound to.  So the paper size will first need to be available from the printer setup in Windows server.  In Windows XP or web server,  go to Start|Printers and Faxes and make sure that the printer you are using has the custom paper size available.

Now for the embedded report designer in Visual Studio .NET, you would right click on the report surface to bring up the context menu and select Designer|Page Setup.  From here select the appropriate printer and paper size.

Above thing is possible on development machine where complete setup of visual studio is installed. But on Live web server, it is not possible to bind printer to crystal report at design time.

If you change the printer name of a report, it will screw your configuration & custom paper size setup.

Solution :

Step : 1

First add your custom paper size on web server using server properties, Create a new form and save with new name.

Step : 2

Add below code in your project page where report will generate pdf file.

System.Drawing.Printing.PrintDocument doctoprint = new System.Drawing.Printing.PrintDocument();
doctoprint.PrinterSettings.PrinterName=”Microsoft XPS Document Writer”;
int i=0;
for (i = 0; i <= doctoprint.PrinterSettings.PaperSizes.Count; i++)
{
int rawKind = 0;
if (doctoprint.PrinterSettings.PaperSizes[i].PaperName == “CHEQUEPRINT”)
{
rawKind = Convert.ToInt32(doctoprint.PrinterSettings.PaperSizes[i].GetType().GetField(“kind”, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(doctoprint.PrinterSettings.PaperSizes[i]));
rd.PrintOptions.PaperSize = (CrystalDecisions.Shared.PaperSize)rawKind;
break; 
}
}

rd.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, filename);
rep_url = objconfig.GetVirRptPath;
General.OpenWindow(rep_url + rep_file, rep_file.Substring(0, rep_file.Length – 4), this.Page);

In above code example, I have created new custom paper size, named “CHEQUEPRINT” having paper size width : 4.13 inch and height : 8.40 inch.

The fact is that crystal report save the information of the format of a report in a non-portable way!
It saves the “ID” of the format instead of his “Name”!
The problem is that in Windows this ID is machine dependent. So here I have fetched Papersize id and assigned to report document.