• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 976
  • Last Modified:

.Net VB Crystal Reports Passing more than one Pram

.Net VB Crystal Reports Passing more than one Pram

I have a report that I need to pass 2 parameters to it.

@form_id as int
@reason as string

I have some code but can't seem to get the 2nd parameters to work.
Private Sub yes2_brn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yes2_brn.Click
        'Reprint Question 2 Yes (Form Lost)
        'SQL and Crystal Print with reason
 
        ''Create an instance of the strongly-typed report object
        crReportDocument = New Crystal_Report18130_land
 
        ''Get the collection of parameters from the report
        crParameterFieldDefinitions = crReportDocument.DataDefinition.ParameterFields
        ''Access the specified parameter from the collection
        crParameterFieldDefinition = crParameterFieldDefinitions.Item("@form_id")
 
        ''Get the current values from the parameter field.  At this point
        ''there are zero values set.
        crParameterValues = crParameterFieldDefinition.CurrentValues
 
        ''Set the current values for the parameter field
        crParameterDiscreteValue = New ParameterDiscreteValue()
        crParameterDiscreteValue.Value = Me.form_id_lbl.Text '1st current value
 
        ''Add the first current value for the parameter field
        crParameterValues.Add(crParameterDiscreteValue)
 
        ''All current parameter values must be applied for the parameter field.
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
 
        ''Use error handling in case an error occurs
        Try
            ''Set the printer name to print the report to.  By default the sample
            ''report does not have a defult printer specified.  This will tell the
            ''engine to use the specified printer to print the report.  Print out 
            ''a test page (from Printer properties) to get the correct value.
            crReportDocument.PrintOptions.PrinterName = My.Settings.Printer
 
            ''Start the printing process.  Provide details of the print job
            ''using the arguments.
            crReportDocument.PrintToPrinter(1, True, 1, 0)
 
            ''Let the user know that the print job is completed.
            MessageBox.Show("finished printing!")
 
        Catch err As Exception
            MessageBox.Show(err.ToString())
        End Try
 
    End Sub

Open in new window

0
kwitcom
Asked:
kwitcom
  • 4
  • 3
  • 2
4 Solutions
 
UnifiedISCommented:
Am I missing the part where you assign the value for your 2nd parameter?  It looks like you only coded for @form_id.

I prefer to use a for/next loop with CR parameters and when hard-coding the parameters, a select/case helps avoid repetitive code

For Each ParamField As CrystalDecisions.Shared.ParameterField In ParamFields
            ParamValues = ParamField.CurrentValues
                        ParamDiscValue = New CrystalDecisions.Shared.ParameterDiscreteValue
SELECT CASE ParamField.Name

CASE @form_id
ParamDiscValue = Me.form_id_lbl.Text
CASE @reason
ParamDiscValue = "something"
END SELECT

ParamValues.Add(ParamDiscValue)
Next
0
 
mlmccCommented:
Parameters should be named ?param not @param.

Are these stored procedure parameters?

mlmcc
0
 
kwitcomAuthor Commented:
Getting following error

Error      1      Value of type 'String' cannot be converted to 'CrystalDecisions.Shared.ParameterDiscreteValue'.      

on Lines:
                Case "@form_id"
                    crParameterDiscreteValue = Me.form_id_lbl.Text
                Case "@reason"
                    crParameterDiscreteValue = "something"

below is the full code and how I changed to meet what you are saying
   Private Sub yes2_brn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yes2_brn.Click
        ''Reprint Question 2 Yes (Form Lost)
        ''SQL and Crystal Print with reason
 
        ''Create an instance of the strongly-typed report object
        crReportDocument = New Crystal_Report18130_land
 
        ''Get the collection of parameters from the report
        crParameterFieldDefinitions = crReportDocument.DataDefinition.ParameterFields
        ''Access the specified parameter from the collection
        crParameterFieldDefinition = crParameterFieldDefinitions.Item("@form_id")
 
 
        For Each ParamField As CrystalDecisions.Shared.ParameterField In crParameterFieldDefinitions
            crParameterValues = ParamField.CurrentValues
            crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
            Select Case ParamField.Name
 
                Case "@form_id"
                    crParameterDiscreteValue = Me.form_id_lbl.Text
                Case "@reason"
                    crParameterDiscreteValue = "something"
            End Select
 
            crParameterValues.Add(crParameterDiscreteValue)
        Next
 
 
        ''Get the current values from the parameter field.  At this point
        ''there are zero values set.
        crParameterValues = crParameterFieldDefinition.CurrentValues
 
        ''Set the current values for the parameter field
        crParameterDiscreteValue = New ParameterDiscreteValue()
        crParameterDiscreteValue.Value = Me.form_id_lbl.Text '1st current value
 
        ''Add the first current value for the parameter field
        crParameterValues.Add(crParameterDiscreteValue)
 
        ''All current parameter values must be applied for the parameter field.
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
 
        ''Use error handling in case an error occurs
        Try
            ''Set the printer name to print the report to.  By default the sample
            ''report does not have a defult printer specified.  This will tell the
            ''engine to use the specified printer to print the report.  Print out 
            ''a test page (from Printer properties) to get the correct value.
            crReportDocument.PrintOptions.PrinterName = My.Settings.Printer8130
 
            ''Start the printing process.  Provide details of the print job
            ''using the arguments.
            crReportDocument.PrintToPrinter(1, True, 1, 0)
 
            ''Let the user know that the print job is completed.
            MessageBox.Show("8130 finished printing!")
 
        Catch err As Exception
            MessageBox.Show(err.ToString())
        End Try
 
    End Sub

Open in new window

0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
mlmccCommented:
Is that when you compile or run the program?

mlmcc
0
 
UnifiedISCommented:
Oops, forget the .value part

 Select Case ParamField.Name
 
                Case "@form_id"
                    crParameterDiscreteValue.value = Me.form_id_lbl.Text
                Case "@reason"
                    crParameterDiscreteValue.value = "something"
            End Select

0
 
kwitcomAuthor Commented:
I am getting the following error now when running.  Also I have added the new code. Still trying to debug the error.

System.InvalidCastException was unhandled
  Message="Unable to cast object of type 'CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition' to type 'CrystalDecisions.Shared.ParameterField'."
  Source="8130"
  StackTrace:
       at WindowsApplication1.reprint8130.yes2_brn_Click(Object sender, EventArgs e) in C:\Documents and Settings\jrwdev\Desktop\Work - TCM\8130\8130\reprint8130.vb:line 461    at System.Windows.Forms.Control.OnClick(EventArgs e)    at System.Windows.Forms.Button.OnClick(EventArgs e)    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)    at System.Windows.Forms.Control.WndProc(Message& m)    at System.Windows.Forms.ButtonBase.WndProc(Message& m)    at System.Windows.Forms.Button.WndProc(Message& m)    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)    at System.Windows.Forms.Application.Run(ApplicationContext context)    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)    at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81    at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)    at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

    Private Sub yes2_brn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yes2_brn.Click
 
        crReportDocument = New Crystal_Report18130_land
        crParameterFieldDefinitions = crReportDocument.DataDefinition.ParameterFields
        'crParameterFieldDefinition = crParameterFieldDefinitions.Item("@form_id")
 
        For Each ParamField As CrystalDecisions.Shared.ParameterField In crParameterFieldDefinitions
            crParameterValues = ParamField.CurrentValues
            crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
            
			Select Case ParamField.Name
                Case "@form_id"
                    crParameterDiscreteValue.Value = Me.form_id_lbl.Text
                Case "@reason"
                    crParameterDiscreteValue.Value = "something"
            End Select
 
            crParameterValues.Add(crParameterDiscreteValue)
        Next
 
        'crParameterValues = crParameterFieldDefinition.CurrentValues
        'crParameterDiscreteValue = New ParameterDiscreteValue()
        'crParameterDiscreteValue.Value = Me.form_id_lbl.Text '1st current value
        'crParameterValues.Add(crParameterDiscreteValue)
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
 
        Try
 
            crReportDocument.PrintOptions.PrinterName = My.Settings.Printer8130
            crReportDocument.PrintToPrinter(1, True, 1, 0)
            MessageBox.Show("8130 finished printing!")
 
        Catch err As Exception
            MessageBox.Show(err.ToString())
        End Try
 
    End Sub

Open in new window

0
 
UnifiedISCommented:
Replace line 4 (crParameterFieldDefinitions = crReportDocument.DataDefinition.ParameterFields
)
with

crParameterFieldDefinitions = crReportDocument.ParameterFields

The parameterfields under the datadefinition namespace are different.
0
 
kwitcomAuthor Commented:
Thnx Guys I got this to work.  So much easier to just repeat the apply.
    Private Sub yes2_brn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yes2_brn.Click
        ''Reprint Question 2 Yes (Form Lost)
        ''SQL and Crystal Print with reason
 
        ''Create an instance of the strongly-typed report object
        crReportDocument = New r8130_land
 
        ''Get the collection of parameters from the report
        crParameterFieldDefinitions = crReportDocument.DataDefinition.ParameterFields
        ''Access the specified parameter from the collection
 
        crParameterFieldDefinition = crParameterFieldDefinitions.Item("@form_id")
        crParameterValues = crParameterFieldDefinition.CurrentValues
        crParameterDiscreteValue = New ParameterDiscreteValue()
        crParameterDiscreteValue.Value = Me.form_id_lbl.Text
        crParameterValues.Add(crParameterDiscreteValue)
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
 
        crParameterFieldDefinition = crParameterFieldDefinitions.Item("@reason")
        crParameterValues = crParameterFieldDefinition.CurrentValues
        crParameterDiscreteValue = New ParameterDiscreteValue()
        crParameterDiscreteValue.Value = "reprint"
        crParameterValues.Add(crParameterDiscreteValue)
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
 
        Try
            ''Set the printer name to print the report to.  By default the sample
            ''report does not have a defult printer specified.  This will tell the
            ''engine to use the specified printer to print the report.  Print out 
            ''a test page (from Printer properties) to get the correct value.
            crReportDocument.PrintOptions.PrinterName = My.Settings.Printer8130
 
            ''Start the printing process.  Provide details of the print job
            ''using the arguments.
            crReportDocument.PrintToPrinter(1, True, 1, 0)
 
            ''Let the user know that the print job is completed.
            MessageBox.Show("8130 finished printing!")
 
        Catch err As Exception
            MessageBox.Show(err.ToString())
        End Try
 
        Me.Close()
 
 
    End Sub

Open in new window

0
 
UnifiedISCommented:
Glad it's working.  I choose to use the for/next loop because I feed the parameters from a database and need to match them by the name.  Certainly hard-coding it for your circumstances will serve you well.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now