Abstract: The purpose of this article will explore how to use the selection formula field to the user selection condition or values from the GUI with the ASP.NET. For example user needs to select a department to generate the report to view information only in that particular department. There are two ways to do this. First is use the parameter, second ways use the selection formula field in report document. We can call this filter data to generate report. Technologies: Crystal Report and ASP.NET Language: C# and VB.NET Prerequisite: Visual Studio 2005/2008 and Crystal report. Implementation In my previous article (Creating report with multi tables' information in ASP.NET) I have written how to generate crystal report with multiple tables. In that article we took a paid salary details report, for all department in the company. Here I am going to show you the same report for a particular department that user's selection. to read my previous article just click below. Creating Crystal Report with Multiple Tables in ASP.NET Follow the same steps and create the report. Use web project name as 'SelectionFormulaReportinASP' and report name 'SelectionFormulaReprot'. Your report will be like this. 
Now you need to add crystal report viewer and a button on the ASP.NET Webpage. Let us see, how we can add the report viewer on the page, just look at these html tags. Html Code: {codecitation class="brush: c#; gutter: true;" width="700px"} {/codecitation} Create a table in form object. Then add a html row and a column.Then place the crystalReportViewer to the column as shown above html tag.If we complie this application, it will give a complier error "CR doesn't exist" when you haven't register Crystal report Namespace. Therefore, you need to register crystal report namespace on the top of the page as shown below. {codecitation class="brush: c#; gutter: true;" width="700px"} {/codecitation} Note: when you double click on toolbox in the crystalreportviewer Visual studio will do all registeration and etc. Now write few lines to display report on the viewer. {codecitation class="brush: c#; gutter: true;" width="700px"} protected void btnLoad_Click(object sender, EventArgs e) { // attached our report to viewer and set database login. ReportDocument report = new ReportDocument(); report.Load(Server.MapPath("SelectionFormulaReprot.rpt")); report.SetDatabaseLogon("username", " pwd",@"server", " database"); rptviewer.SelectionFormula ="{Department.Department} = '" + txtDepartment.Text + "'"; rptviewer.ReportSource = report; } {/codecitation} Note:When you give selection formula to report viewer you need take care of followings 1.you must use the single equal size , when you are check you're your data is equal 2.You always {Table.CoulmnName } syntax 3.when you check char values make sure you have to put values within the ' ' mark. Here I have writen code under the btnLoad_Click event. On my code just create a report document object and load report from the path. MapPath() method to get absolute path from the report file name. Then build the web application and run, you will get result like follow. 
Conclusion: This article is focused about creating report with selection formula. I hope this article helpful to you. |