Pass Crystal Report Parameters Programmatically In Asp.Net 2.0,3.5,4.0 Using C# And VB.NET. In this post i am explaining how to pass parameters to crystal reports programmatically in code behind of asp.net web page.
For this example 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.
Read how to create Crystal Reports In Asp.Net , Or Windows Forms.
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 promptingand 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.
1
using
CrystalDecisions.Shared;
2
using
CrystalDecisions.CrystalReports.Engine;
Write this code in Page_Load event of the page
1
protected
void
Page_Load(
object
sender, EventArgs e)
2
{
3
if
(Page.IsPostBack) CrystalReportViewer1.Visible =
true
;
4
else
5
CrystalReportViewer1.Visible =
false
;
6
}
Generate click event for button to shaow report and write this code.
01
protected
void
btnReport_Click(
object
sender, EventArgs e)
02
{
//Create report document
03
ReportDocument crystalReport =
new
ReportDocument();
04
05
//Load crystal report made in design view
06
crystalReport.Load(Server.MapPath(
"CrystalReport.rpt"
));
07
08
//Set DataBase Login Info
09
crystalReport.SetDatabaseLogon
10
(
"amitjain"
,
"password"
,
@"AMITJAIN\SQL"
,
"Northwind"
);
11
12
//Provide parameter values
13
crystalReport.SetParameterValue(
"ProductID"
, txtProductID.Text);
14
CrystalReportViewer1.ReportSource = crystalReport;
15
}
No comments:
Post a Comment