Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Pass parameters to Crystal Reports in asp.net

Jan 20, 2012
Introduction:

In this article I will explain how to pass parameters to crystal reports programmatically 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.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

33 comments :

Anonymous said...

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.

Roja Samala said...
This comment has been removed by the author.
make iphone app said...

I would understand nothing without the screenshots! Thanks!

Anonymous said...

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.

Suresh Dasari said...

To solve that problem check this post http://www.aspdotnet-suresh.com/2012/02/crystal-report-not-showing-data-when.html

Anonymous said...

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.

Anonymous said...

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?

darshan said...

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

darshan said...

Error is coming of invalid string format..
how do i convert this into proper string format

qnhata3 said...

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!

Anonymous said...

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")

beema said...

reportdocument.SetParameterValue("APP_NO", txtdob.Text);
i am getting following wrror
Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))

dasu said...
This comment has been removed by the author.
Dave Ash said...

Thanks for the tips. I'm new to this area, so your information is giving me a kickstart. Thank you.
http://eigo.co.uk

sravani nalluri said...

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

Anonymous said...

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

Anonymous said...

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

Rajkumar Palnati said...

Is it possible to export this data into excel or pdf ?

Thanks in advance Bro

Anonymous said...

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

Subhash Potbhare said...

this is just great........

Mohammad Al Mamun Sumon said...

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.

Mohammad Al Mamun Sumon said...

Solved my previous question. Now I want to know how to pass multiple parameters? Please answer.

Karuppa Samy said...

Hi suresh,
please give sql commands crystal report.

Anonymous said...

Hi suresh..

Fantastic Site...Easy to understand..thanks..

mobile apps development said...

yes absolutely this is fantastic one keep going.

Nathan Beesley said...

Dont forget the "@" in front of the Param name. For example reportDocument.SetParameterValue("@ParamName",value);

karthikeyan karthikeyan said...

sir how to pass multiple parameters to crystal reports?

Edwin K said...

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.

karthikeyan karthikeyan said...

i am karthi......
sir single parameter pass is working.....but how to pass multiple parameter to report????????

Anonymous said...

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...

Smm Friend said...

Thank you very much Suresh...!
Naa problem anta meeru chal easy ga solve chesaaru..
thank you very much....

lingmaaki said...

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

Anonymous said...

hi, i have a report containing a sub report.
how can i pass parameters to both at a same time?
tq

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.