Pass crystal report parameters programmatically in Asp.Net 2.0,3.5.
In this post i am explaining how to pass parameters to crystal reports programmatically in code behind of asp.net web page.
For this i am using northwind database and products table.
I have put one text box on the page and report will display details of product based on product id entered by user.
To know how to create crystal report in Asp.Net .
If u want to know how to create crystal reports with parameters in winforms or windows application then read this.
Open crystal report in design view, right click on it and select Field Explorer
Now select Parameter Fields and select new to add new parameter to report.
Name it as ProductID and remember it.
Now click on Special Fields in Field Explorer and select Record Selection Formula.
Select is equal to and {?ProductID} from the dropdowns and click on OK.
Click on smart tag of reportviewer control and uncheck Database logon prompting and parameter prompting as we will provide these info in code behind.
HTML markup of aspx page
<form id="form1" runat="server"> <table class="style1"> <tr> <td> Enter Product ID : </td> <td> <asp:TextBox ID="txtProductID" runat="server"> </asp:TextBox> </td> <td> <asp:Button ID="btnReport" runat="server" Text="Show Report" onclick="btnReport_Click" Width="108px" /> </td> </tr> </table> <br /> <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="True" EnableDatabaseLogonPrompt="False" EnableParameterPrompt="False" Height="1039px" ReportSourceID="CrystalReportSource1" ReuseParameterValuesOnRefresh="True" Width="901px" DisplayGroupTree="False" /> <CR:CrystalReportSource ID="CrystalReportSource1" runat="server"> <Report FileName="CrystalReport.rpt"> </Report> </CR:CrystalReportSource> </form>
Now go to code behind of the page and add below mentioned namespace for crystal reports.
1using CrystalDecisions.Shared;2using CrystalDecisions.CrystalReports.Engine;Write this code in Page_Load event of the page
1protected void Page_Load(object sender, EventArgs e)2 {3 if (Page.IsPostBack) CrystalReportViewer1.Visible = true;4 else5 CrystalReportViewer1.Visible = false;6 }Generate click event for button to shaow report and write this code.
01protected void btnReport_Click(object sender, EventArgs e)02 { //Create report document03 ReportDocument crystalReport = new ReportDocument();04 05 //Load crystal report made in design view06 crystalReport.Load(Server.MapPath("CrystalReport.rpt"));07 08 //Set DataBase Login Info09 crystalReport.SetDatabaseLogon10 ("amitjain", "password", @"AMITJAIN\SQL", "Northwind");11 12 //Provide parameter values13 crystalReport.SetParameterValue("ProductID", txtProductID.Text);14 CrystalReportViewer1.ReportSource = crystalReport;15 }Build the solution and run.

No comments:
Post a Comment