Introduction:
In this article I will explain how to pass parameters to crystal reports programmatically in asp.net.
In this article I will explain how to pass parameters to crystal reports programmatically in asp.net.
Description:
In Previous posts I explained how to install crystal reports in visual studio 2010 and crystal reports example in asp.net.
In Previous posts I explained how to install crystal reports in visual studio 2010 and crystal reports example in asp.net.
After completion of crystal report creation using crystal reports example in asp.net open crystal report in design mode. Now in Field Explorer select Parameter Fields and right click on it and select New
After select New one window will open in that enter Name of new parameter, select value Type and enter Prompting Text and click OK
Our new Parameter field will be added under Parameter Fields in Field Explorer. Now click on Select Export in toolbar section as highlighted in red
When we click Select Expert Choose Field window will open in that select your field click OK Here I am selecting UserName because I want to display the details based on UserName
After click on OK button Select Export window will open in that select operator type as “is equal to” and select “{?Username}” from dropdowns and click OK
Our report ready with parameter now we can access the user details based on Username.
Now open CrystalReportViewer Control click on right hand side of smart tag in that uncheck Enable Report Parameter Prompting option because if we run our application that will prompt window for UserName every time
After assign report to CrystalReportViewer control add one textbox and button control to your aspx page after add those controls our page will be like this
<%@ Register Assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Crystal Report with parameters</title> </head> <body> <form id="form1" runat="server"> <table> <tr> <td><b>Enter UserName</b></td> <td><asp:TextBox ID="txtUserName" runat="server"/></td> </tr> <tr> <td></td> <td><asp:Button ID="btnSearch" runat="server" Text="Search" onclick="btnSearch_Click" /></td> </tr> </table> <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" EnableDatabaseLogonPrompt="False" EnableParameterPrompt="False" /> </form> </body> </html> |
Now Open your code behind file and set database connection settings and assign reports to the control before that first add following namespaces
using System; using CrystalDecisions.CrystalReports.Engine; |
After add namespaces write the following code in code behind
C# code
protected void Page_Load(object sender, EventArgs e) { } protected void btnSearch_Click(object sender, EventArgs e) { ReportDocument reportdocument = new ReportDocument(); reportdocument.Load(Server.MapPath("CrystalReport.rpt")); reportdocument.SetDatabaseLogon("", "", "SureshDasari", "MySampleDB"); reportdocument.SetParameterValue("Username", txtUserName.Text); CrystalReportViewer1.ReportSource = reportdocument; } |
VB.NET Code
Imports CrystalDecisions.CrystalReports.Engine Partial Class Default2 Inherits System.Web.UI.Page Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load End Sub Protected Sub btnSearch_Click(sender As Object, e As EventArgs) Dim reportdocument As New ReportDocument() reportdocument.Load(Server.MapPath("CrystalReport.rpt")) reportdocument.SetDatabaseLogon("", "", "SureshDasari", "MySampleDB") reportdocument.SetParameterValue("Username", txtUserName.Text) CrystalReportViewer1.ReportSource = reportdocument End Sub End Class |
Now run your application and check your output that would be like this
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
43 comments :
I continue to receive Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX)) and it errors out onCrystalReportViewer1.ReportSource = reportdocument
Please help me figure this out. I get to the parameter page, but then Poof, blows up on search.
I would understand nothing without the screenshots! Thanks!
thanks for wonderfull post
i have follow all steps and created report but when i click on print button then report page was clear. please help me.
To solve that problem check this post http://www.aspdotnet-suresh.com/2012/02/crystal-report-not-showing-data-when.html
Hello, thanks for the info, its very helpful, I just have one problem. It says "Server does not exist in the current context" when typing Server.MapPath.
I had my report working fine, thanks to your help, but I cannot add a new parameter with the method shown here (reportdocument.setparametervalue...), when I run the program it says that I need to initialize the object with the word "new". What is going on?
I have one doubts
my server name is
DARSHAN-PC\SQLEXPRESS
when i write this as a string i am getting error..please help me how to solve this problem
thanks
Error is coming of invalid string format..
how do i convert this into proper string format
Hi,
I have a question about crystal report in visual studio 2008. I use object crystal report viewer for show my report:
crystalReportViewer.ReportSource = reportDocument;
crystalReportViewer.ParameterFieldInfo = report.ParameterFields;
crystalReportViewer.Refresh();
When I change parameter of report on user interface , memory of program is increase about 3-5 MB. How can I clean old data before change parameter ? I don't know where is data of report. Thanks so much!
Thanks a lot...its was nice but at the time of login and password he is saying login failled
like in this line
------------------------------------------
reportdocument.SetDatabaseLogon("", "", "SureshDasari", "MySampleDB")
reportdocument.SetParameterValue("APP_NO", txtdob.Text);
i am getting following wrror
Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))
Thanks for the tips. I'm new to this area, so your information is giving me a kickstart. Thank you.
http://eigo.co.uk
hello suresh
i am passsing parameters in crystalreports in c# at runtime then i am getting the exception Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))
i followed your tutorials still i am getting the same exception
here is my code
productwisedetailed crs = new productwisedetailed();
crs.Load ("D:\\excel123\\excel123\\productwisedetailed.rpt");
crs.SetDatabaseLogon("sa", "123");
crystalReportViewer1.ReportSource = crs;
crs.SetParameterValue("product", prodct.Text); //here i got the exception
crystalReportViewer1.ReportSource = crs;
plz help me
thanks in advance
thanxxxxxxxxxxxxxxxxxxxxxxxx!!!!!!!!!. You gave me a santa papada code when i was at the centre of pacific ocean. from my side you are promoted to your next upper chair. thanks again.
pintu.gangu@yahoo.com
this is nice example
how about if i want to load data in load page event
i mean while textbox is null load all data from table(UserInfo) in my report else
load only written in textbox
i have solution share fro all people
after create parameter >>select select expert
and wwrite this formula
{UserInfo.Username} = {?Username} or ( {Username}="")
thank you very much sir
Is it possible to export this data into excel or pdf ?
Thanks in advance Bro
thank you suresh your coding helped me alot in passing parameters but iam unable to move to next page it is showing error
no valid source to report at the same time un able to export the report.can you please help me
this is just great........
Dear Suresh,
When I browse report on design time, I get data as I designed the report. But when I run my full application like "http://www.myapp.com/rpt.aspx" or "111.111.111.111/rpt.aspx" it shows nothing but a blank page. Plz solve the problem.
Solved my previous question. Now I want to know how to pass multiple parameters? Please answer.
Hi suresh,
please give sql commands crystal report.
Hi suresh..
Fantastic Site...Easy to understand..thanks..
yes absolutely this is fantastic one keep going.
Dont forget the "@" in front of the Param name. For example reportDocument.SetParameterValue("@ParamName",value);
sir how to pass multiple parameters to crystal reports?
Hello suresh dasari am a newbie in Crystal reports and Visual Studio 2008 aspx, i have followed your instructions on the above example all has worked well. The issue i have right now is Selecting or Passing a range of records to crystal reports from a web form. ie i have students 1 to 10 and would like to show the records of students 5 to 10 on crystal reports by select records from 2 dropdown lists
(DropDownList1 = From (5) and DropdownList2 = To (10)).
Thanks alot for your help.
i am karthi......
sir single parameter pass is working.....but how to pass multiple parameter to report????????
parameter is working...but why i can't print my crystal report parameter ??
print button and export in toolbox not working....
I am using vb net 2005...
Thank you very much Suresh...!
Naa problem anta meeru chal easy ga solve chesaaru..
thank you very much....
You should create ParameterFieldDefinitions before passing arguments
ParameterFieldDefinitions crParameterFieldDefinitions ;
ParameterFieldDefinition crParameterFieldDefinition ;
ParameterValues crParameterValues = new ParameterValues();
ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();
crParameterDiscreteValue.Value = textBox1.Text;
crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
crParameterFieldDefinition = crParameterFieldDefinitions["fromDate"];
crParameterValues = crParameterFieldDefinition.CurrentValues;
crParameterValues.Clear();
crParameterValues.Add(crParameterDiscreteValue);
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);
crParameterDiscreteValue.Value = textBox2.Text;
crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
crParameterFieldDefinition = crParameterFieldDefinitions["toDate"];
crParameterValues = crParameterFieldDefinition.CurrentValues;
crParameterValues.Add(crParameterDiscreteValue);
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);
source:
http://csharp.net-informations.com/crystal-reports/csharp-crystal-reports-date-to-date.htm
ling
hi, i have a report containing a sub report.
how can i pass parameters to both at a same time?
tq
how to export a excel sheet in a particular name in crystal report 2.0?lz help it's emergency by deepak
when i'll enter it will create error...how to fix???
nice nice, it is really helpful to me.
Hi
I am getting an Error:
Database Logon Failed.
Please Help me to solve this problem.
Hi
I have a report which is Designed in Crystal Report 2008 and I have imported that report into VS.
I am getting an Error:
Database Logon Failed.
while executing the web page.
Please Help me to solve this problem.
im not technically strong but after seeing those posts and these much suggestion codes , i kinda started to like coding. thank you so much
great , informative post. thanks
Although I am getting a parameter from QueryString and not a textbox, I got my report working perfectly the first time I followed the principals outlined in your example. Thank you for the clear and easy instructions. God bless.
Note: Only a member of this blog may post a comment.