Add any customize text in crystal reports by .net code

In my requirement I have to show few search parameter in crystal reports which is passed from aspx page.
And that is not hard coded fixed because I have 10-15 parameters and I want to place it dynamically if whatever parameter’s value available that only needs to be attach in reports.

So I have implemented this by below approaches.
In Crystal reports rpt file I have take one default fields with “SearchTextRight” and “SearchTextLeft” to display value left and right side corner.

This both fields “CanGrouw” property set as “True” and attach each of the search parameter by “\n” so automatically new line added and text object’s height will increased.
Below are the code we have used for that.

 ReportClass rptH = GenerateReportClasss(Server.MapPath("~/Reports/GetReortData.rpt"));
            
            /// Attach serarch fields

            SearchDraw oSearchDrow = AttachCustomerHours(collection);
            if (oSearchDrow.SearchingStringLeft != string.Empty)
            {
                TextObject SeachTextLeft = (TextObject)rptH.ReportDefinition.ReportObjects["SearchTextLeft"];
                SeachTextLeft.Text = oSearchDrow.SearchingStringLeft;
            }
            if (oSearchDrow.SearchingStringRight != string.Empty)
            {
                TextObject SeachTextRight = (TextObject)rptH.ReportDefinition.ReportObjects["SearchTextRight"];
                SeachTextRight.Text = oSearchDrow.SearchingStringRight;
            }
            AttachLocalizationCustomerHours(rptH);
            rptH.Refresh();

            rptH.SetDataSource(Ds.Tables[0]);

Now all text will attached in reports’ PDF or excel file.

Let me know for more details

Thanks,
Amit Patel
amitpatel.it@gmail.com
“Enjoy Programming”

About these ads

Implement Multi Lingual support in Crystal Reports by .net code

This article is targeting to .net developer whom they are dealing with crystal reports.
Generally we are working multi language supported application where we are using resource file to fetch value different language and they are loaded in UI based on users culture.

But this structure is not working in crystal reports. In crystal reports generally we are filling all data from database but what about headers and title information, if client requires that all available in based on selected language.

So for this handle we need to fill each text objects in crystal reports. So we have to provided appropriate name of all headers and title related text object in rtp file.
Below are the code for find that find and update all objects.

ReportClass reortptH = new ReportClass();
            rptH.FileName = Server.MapPath("~/Reports/ReportData.rpt");

reortptH.Load();


            
TextObject EmployeeName = (TextObject)reortptH.ReportDefinition.ReportObjects["hdEmployeeName"];
EmployeeName.Text = ResourceManager.GetObject(“EmployeeName”); // EmployeeName is resource key 

TextObject Department = (TextObject)reortptH.ReportDefinition.ReportObjects["hdDepartment"];
Department.Text = ResourceManager.GetObject(“Department”); // Department is resource key 

//……Need to update all text objects with resource key value.
reortptH.Refresh();

So in this way we can update available header and title related static information reports by value from resource file. We can create a report in multi-language supported.

Please add comments if you have any suggestion/questions.

Thanks,
Amit Patel
amitpatel.it@gmail.com
“Enjoy Programming”

Crystal Reports in MVC

Below are the code for implement crystal reports in MVC with support of export to PDF and Excel format.

MVC rajor/aspx pages is not supporting report viewer so that is not possible to place report viewer where user can see reports and export that in required format.
So in MVC we have directly deal with report class and generate PDF/Excel and sent to client with browsers functionality view and download.

View: In view required all search parameter and with Generate report button (PDF/Excel)

Contollor : based on provided search parameter fetch data and bind with crystal reports class and generate PDF/excel and sent to client’s browser for download.

Below the are the code for controller to generate and send file to client’s browser.

ReportsServices dbReportsServices = new ReportsServices();
ReportClass reortptH = new ReportClass();
            reortptH.FileName = Server.MapPath("~/Reports/ReportData.rpt");

reortptH.Load();

DataSet Ds = dbReportsServices.FetchData("GetData"); // exectures store procedure to retrieve data

reortptH.SetDataSource(Ds.Tables[0]);

bool IsPDF = Convert.ToBoolean(collection.Get("IsPDF")); // In view add two button for export to PDF and excel and that will returs value on that way.

            if (IsPDF)
            {
                System.IO.Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
                return File(stream, "application/pdf", "ReportData.pdf");
            }
            else
            {
                System.IO.Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.Excel);
                return File(stream, "application/xls", "ReportData.xls");
            }

Post your comments if you have questions/comments.

Thanks,
Amit Patel
amitpatel.it@gmail.com
“Enjoy Programming”

Follow

Get every new post delivered to your Inbox.

Join 239 other followers