How to pass parameter from VB.Net to Crystal Report 
Author Message
 How to pass parameter from VB.Net to Crystal Report

I had the same question as that posed  at
http://www.*-*-*.com/ +parameter+group:microsoft.publ....
 That is, how do you get a string like "Between the months of January
and June" to show up in the heading of a Crystal Report in VB.Net.
The responses to his/her question were very helpful in steering me to
the right place, but I had to do some additional digging to find a
final solution.

Step 1:  Add a parameter field to your report object.  This is done by
going into the Crystal Report Field Explorer, which you should see
when you open your report in design view.  If you don't see it, press
Ctrl + Alt + L.  Right-click on Parameter Fields and select New...
from the popup menu.  I just filled in the name "Subheading1" and left
everything else at its default position.  Add the newly created
parameter field to your report by dragging it from the Field Explorer
to your report.

Step 2:  Modify your code to add a parameterfields collection to the
report viewer.  In my case I created a form called ReportViewer.  It
is a generic form that I will use to preview many different reports.
On that form I created a Crystal Report Viewer, called
CrystalReportViewer1.  My first Crystal Report is called rptReport1.

Starting at the top of the process, I call this code in my "Run Report
1" button:

        Dim f2 As New ReportViewer() 'Open my form
        Dim rpt1 As New rptReport1() 'Open my Crystal Report object

        Try
            Me.Cursor = Cursors.WaitCursor 'Set cursor to hourglass
            rpt1.SetDataSource(ds.Tables(0)) 'Set the data source of
my report object
            f2.ReportSource = rpt1 'Pass the report to the form via a
report property shown below.
            'Pass in the parameter information via the method shown
below.
            f2.ParameterAdd("Subheading1", "Between the months of
January and June")
        Catch myexecp As Exception
            Dim strTemp As String
            strTemp = "An error has occurred trying to open this
report:" & vbCrLf & vbCrLf & _
            myexecp.ToString()
            MsgBox(strTemp)
            Exit Sub
        Finally
            Me.Cursor = Cursors.Default
        End Try

The code behind my report viewer form looks like this:

Imports CrystalDecisions.Shared

Public Class ReportViewer
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Dim mg_objReportSource As Object
    Dim mg_arystr(10, 2) As String
    Dim mg_intAryNextPos As Integer = 0

    'Property to receive and store the report object.
    Public Property ReportSource() As Object
        Get
            Return mg_objReportSource
        End Get
        Set(ByVal Value As Object)
            mg_objReportSource = Value
        End Set
    End Property

    'Method to receive and store up to ten parameters
    Public Function ParameterAdd(ByVal pstrName As String, ByVal
pstrValue As String)
        If mg_intAryNextPos > UBound(mg_arystr, 1) Then
            MsgBox("Number of permissible report parameters
exceeded.")
            Exit Function
        End If
        mg_arystr(mg_intAryNextPos, 0) = pstrName
        mg_arystr(mg_intAryNextPos, 1) = pstrValue
        mg_intAryNextPos += 1
    End Function

    'The form's load event, that fires everything off.  It is called
when the form's .Show method
    'is called.  IMPORTANT NOTE:  The parameters have to be set before
the ReportSource is, otherwise the
    'user will be prompted to enter one.
    Private Sub ReportViewer_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        ParametersSet()
        Me.CrystalReportViewer1.ReportSource = mg_objReportSource
    End Sub

    'Loads the parameters that have been individually stored with the
ParameterAdd method.
    Private Sub ParametersSet()
        If mg_intAryNextPos = 0 Then Exit Sub

        Dim paramFields As New ParameterFields()
        Dim int1 As Integer
        Dim strName, strValue As String

        For int1 = 0 To mg_intAryNextPos
            strName = mg_arystr(int1, 0)
            strValue = mg_arystr(int1, 1)
            AddParameter(strName, strValue, paramFields)
        Next int1
        Me.CrystalReportViewer1.ParameterFieldInfo = paramFields
    End Sub

    'Thanks to Crystal Reports on-line help for this.  It adds
ParamField objects
    'to the ParameterFields collection.  The collection is then
assigned to the
    'viewer in the sub right above this one.
    Private Sub AddParameter(ByVal paramName As String, _
        ByVal paramValue As String, ByRef paramFields As
ParameterFields)

        Dim paramField As New ParameterField()
        Dim paramDiscreteValue As New ParameterDiscreteValue()
        Dim paramValues As New ParameterValues()

        ' Set the name of the parameter to modify.
        paramField.ParameterFieldName = paramName

        ' Set a value to the parameter.
        paramDiscreteValue.Value = paramValue
        paramValues.Add(paramDiscreteValue)
        paramField.CurrentValues = paramValues

        ' Add the parameter to the ParameterFields collection.
        paramFields.Add(paramField)
    End Sub
End Class

Hope this is helpful!

Karl Hoaglund
Nexus Consulting Group, Inc.
544 E. Ogden Ave #700-110
Milwaukee, WI  53202-2656
414/347-0753



Tue, 19 Jul 2005 08:15:40 GMT  
 How to pass parameter from VB.Net to Crystal Report
If  you are looking for more information on how to use CR.NET, you can read
my free ebook online.

* Covers ASP.NET and Windows Forms
* Teaches all phases of report development
* Alerts you to the bugs and how to get around them
* Maps out the complex ReportDocument object model
* Shows how to perform runtime customization
* Demonstrates all options of connecting with ADO.NET
* Gives complete code examples for every concept presented

www.CrystalReportsBook.com/rdr.asp?cpgn=ngcr_013003

/Brian Bischof


Quote:
> I had the same question as that posed  at

http://groups.google.com/groups?q=pass+parameter+group:microsoft.publ...
rystal&hl=en&lr=&ie=UTF-8&selm=1e9e01c07bdc%242ba26b20%2434862ecf%40cpmsftng
xa05&rnum=1.
Quote:
>  That is, how do you get a string like "Between the months of January
> and June" to show up in the heading of a Crystal Report in VB.Net.
> The responses to his/her question were very helpful in steering me to
> the right place, but I had to do some additional digging to find a
> final solution.

> Step 1:  Add a parameter field to your report object.  This is done by
> going into the Crystal Report Field Explorer, which you should see
> when you open your report in design view.  If you don't see it, press
> Ctrl + Alt + L.  Right-click on Parameter Fields and select New...
> from the popup menu.  I just filled in the name "Subheading1" and left
> everything else at its default position.  Add the newly created
> parameter field to your report by dragging it from the Field Explorer
> to your report.

> Step 2:  Modify your code to add a parameterfields collection to the
> report viewer.  In my case I created a form called ReportViewer.  It
> is a generic form that I will use to preview many different reports.
> On that form I created a Crystal Report Viewer, called
> CrystalReportViewer1.  My first Crystal Report is called rptReport1.

> Starting at the top of the process, I call this code in my "Run Report
> 1" button:

>         Dim f2 As New ReportViewer() 'Open my form
>         Dim rpt1 As New rptReport1() 'Open my Crystal Report object

>         Try
>             Me.Cursor = Cursors.WaitCursor 'Set cursor to hourglass
>             rpt1.SetDataSource(ds.Tables(0)) 'Set the data source of
> my report object
>             f2.ReportSource = rpt1 'Pass the report to the form via a
> report property shown below.
>             'Pass in the parameter information via the method shown
> below.
>             f2.ParameterAdd("Subheading1", "Between the months of
> January and June")
>         Catch myexecp As Exception
>             Dim strTemp As String
>             strTemp = "An error has occurred trying to open this
> report:" & vbCrLf & vbCrLf & _
>             myexecp.ToString()
>             MsgBox(strTemp)
>             Exit Sub
>         Finally
>             Me.Cursor = Cursors.Default
>         End Try

> The code behind my report viewer form looks like this:

> Imports CrystalDecisions.Shared

> Public Class ReportViewer
>     Inherits System.Windows.Forms.Form

> #Region " Windows Form Designer generated code "

>     Dim mg_objReportSource As Object
>     Dim mg_arystr(10, 2) As String
>     Dim mg_intAryNextPos As Integer = 0

>     'Property to receive and store the report object.
>     Public Property ReportSource() As Object
>         Get
>             Return mg_objReportSource
>         End Get
>         Set(ByVal Value As Object)
>             mg_objReportSource = Value
>         End Set
>     End Property

>     'Method to receive and store up to ten parameters
>     Public Function ParameterAdd(ByVal pstrName As String, ByVal
> pstrValue As String)
>         If mg_intAryNextPos > UBound(mg_arystr, 1) Then
>             MsgBox("Number of permissible report parameters
> exceeded.")
>             Exit Function
>         End If
>         mg_arystr(mg_intAryNextPos, 0) = pstrName
>         mg_arystr(mg_intAryNextPos, 1) = pstrValue
>         mg_intAryNextPos += 1
>     End Function

>     'The form's load event, that fires everything off.  It is called
> when the form's .Show method
>     'is called.  IMPORTANT NOTE:  The parameters have to be set before
> the ReportSource is, otherwise the
>     'user will be prompted to enter one.
>     Private Sub ReportViewer_Load(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
>         ParametersSet()
>         Me.CrystalReportViewer1.ReportSource = mg_objReportSource
>     End Sub

>     'Loads the parameters that have been individually stored with the
> ParameterAdd method.
>     Private Sub ParametersSet()
>         If mg_intAryNextPos = 0 Then Exit Sub

>         Dim paramFields As New ParameterFields()
>         Dim int1 As Integer
>         Dim strName, strValue As String

>         For int1 = 0 To mg_intAryNextPos
>             strName = mg_arystr(int1, 0)
>             strValue = mg_arystr(int1, 1)
>             AddParameter(strName, strValue, paramFields)
>         Next int1
>         Me.CrystalReportViewer1.ParameterFieldInfo = paramFields
>     End Sub

>     'Thanks to Crystal Reports on-line help for this.  It adds
> ParamField objects
>     'to the ParameterFields collection.  The collection is then
> assigned to the
>     'viewer in the sub right above this one.
>     Private Sub AddParameter(ByVal paramName As String, _
>         ByVal paramValue As String, ByRef paramFields As
> ParameterFields)

>         Dim paramField As New ParameterField()
>         Dim paramDiscreteValue As New ParameterDiscreteValue()
>         Dim paramValues As New ParameterValues()

>         ' Set the name of the parameter to modify.
>         paramField.ParameterFieldName = paramName

>         ' Set a value to the parameter.
>         paramDiscreteValue.Value = paramValue
>         paramValues.Add(paramDiscreteValue)
>         paramField.CurrentValues = paramValues

>         ' Add the parameter to the ParameterFields collection.
>         paramFields.Add(paramField)
>     End Sub
> End Class

> Hope this is helpful!

> Karl Hoaglund
> Nexus Consulting Group, Inc.
> 544 E. Ogden Ave #700-110
> Milwaukee, WI  53202-2656
> 414/347-0753




Tue, 19 Jul 2005 08:55:50 GMT  
 How to pass parameter from VB.Net to Crystal Report
Ooops.  A couple minor corrections:

1) In the first code sample this line should come at the end:

f2.show

2) In the second code sample, ignore (omit) the line that starts with
#Region.  This is generated by VB.Net.

3)  In the for-next loop that walks though the parameter list, the
ending value should be "mg_intAryNextPos-1", not "mg_intAryNextPos"

Sorry!

Karl



Tue, 19 Jul 2005 19:43:07 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Passing a variable from VB.NET to a Crystal report parameter or formula with no prompt

2. Parameter passing for Crystal Reports in .Net

3. Passing parameters in Crystal reports.net

4. Passing Parameters from C# to Crystal Report / Crystal Report Viewer

5. Crystal Reports 8.5 (VB6) to Crystal Reports NET (VB.NET) conversion

6. Brian Bischof - Help - parameters not passing to Crystal from VB.NET

7. How do you pass parameters from VB 5 to crystal reports

8. Pass Parameter to Crystal Reports from VB

9. Passing SQL Server parameters to subreports in Crystal Reports 6 from VB

10. Problem with VB passing parameters to Crystal Report - Stored Procedure

11. passing Parameter from Vb to Stored Procedure In crystal reports

12. vb problems - parameter passing to crystal reports

 

 
Powered by phpBB® Forum Software