Subscribe to Subscribers
Talk to Webmaster Tony John


Resources » .NET programming » .NET Framework

How to dynamically pass parameters from a vb.net form to crystal reports at runtime in vb.net?


Posted Date:     Category: .NET Framework    
Author: Member Level: Silver    Points: 15


Many a times, we want to pass some values, say value of a label or a textbox to a crystal report query at runtime. Here I will discuss the steps to do the same. Parameters are added to the report query with a preceding '@' symbol and initialized to their default values. And then, they are made to hold the values from the objects like textbox or label using the code mentioned in this article.



 


Suppose we have a label, textbox and button named as label1, textbox1 and button1 respectively in a vb.net application form. On click of the button , we display a crystal report which is connected to the database . The report has the following sql query -

"select * from table_name where field1 = "abc" and field2 = "def"

where table_name is the name of the table and field1 and field2 are two of it's field names.

But at runtime, we want field1 to take the value of label1 and field2 to that of textbox1.

Here I will discuss the steps to pass these parameters to the report dynamically at runtime

Step1: Include these namespaces in the form where you have your crystalreportviewer control

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

Step2: Add two parameters to the report @txt1 and @txt2 to the report and change the query to "select * from table_name where field1 = {?@txt1} and field2 = {?@txt2}"

Step3: come back to the form where you have your crystalreportviewer control and start writing these lines of code



Dim ParamFields As ParameterFields = Me.CrystalReportViewer1.ParameterFieldInfo

'declare two parameterfields

Dim P_txt1 As New ParameterField 'to hold the textbox value
Dim P_txt2 As New ParameterField 'to hold the label value

'setting the variables to the ones declared in the report query
P_txt1.Name = "@txt1"
P_txt2.Name = "@txt2"

Dim P_txt1_Value As New ParameterDiscreteValue
Dim P_txt2_Value As New ParameterDiscreteValue

'Form1 is the name of the form that holds the lablel and button
P_txt1_Value.Value = Form1.label1.text
P_txt2_Value.Value = Form1.textbox1.text

P_txt1.CurrentValues.Add(P_txt1_Value)
P_txt2.CurrentValues.Add(P_txt2_Value)

ParamFields.Add(P_txt1)
ParamFields.Add(P_txt2)



After implementing the code, the report will show the values from the label and textbox at runtime.

Regards,
Pratibha Mohanty





Did you like this resource? Share it with your friends and show your love!


Responses to "How to dynamically pass parameters from a vb.net form to crystal reports at runtime in vb.net?"
Guest Author: Argha Chakraborty     23 Oct 2012
After doing this it is showing "Wrong parameter Value". What to do?


Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Inheritance Puzzle in ASP.NET
    Previous Resource: Comparison of MVC implementation between J2EE Struts 2 & ASP.NET MVC 2, who is the best? Part 2
    Return to Resources
    Post New Resource
    Category: .NET Framework


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Passing parameters  .  Crystal reports  .  Passing parameters at runtime  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.