locked
Cannot Pass Parameters from code to Crystal Reports RRS feed

  • Question

  • User-1721125449 posted

    This is the first time I've ever used Crystal Reports, and it shows.

    In a seperate page, I select a row from a gridview and click a button to print that particular report.

    The Querystring passes that value, the contract number, to the report page. 

    In the rpt, I have a database field ContractNumber and have created a Crystal Reports parameter, @ContractNumber, and associated those in the record selection formula.

    The associated code is:

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim crystalReport As New ReportDocument()
            crystalReport.Load(Server.MapPath("FC15ContractPrint.rpt"))        
            CrystalReportViewer1.ReportSource = crystalReport
            HiddenField1.Value = Request.QueryString("ContractNumber")
    
            Dim crParameterFieldDefinitions As ParameterFieldDefinitions
            Dim crParameterFieldDefinition As ParameterFieldDefinition
            Dim crParameterValues As New ParameterValues
            Dim crParameterDiscreteValue As New ParameterDiscreteValue
    
            crParameterDiscreteValue.Value = HiddenField1.Value
            crParameterFieldDefinitions = crystalReport.DataDefinition.ParameterFields()
            crParameterFieldDefinition = crParameterFieldDefinitions.Item("@ContractNumber")
            crParameterValues = crParameterFieldDefinition.CurrentValues
    
            crParameterValues.Clear()
            crParameterValues.Add(crParameterDiscreteValue)
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
            CrystalReportViewer1.RefreshReport()
        End Sub

    It keeps asking me to manually input the parameter, though.

     

    Monday, December 10, 2012 10:26 AM

Answers

  • User1315711699 posted
    Dim crystalReport As New ReportDocument()
    crystalReport.Load(Server.MapPath("FC15ContractPrint.rpt"))
    // here before setting report source set parameter values
    crystalReport.SetParameterValue("ParameterName",value) // paramtere name should be similar to your crystal report parameter

    CrystalReportViewer1.ReportSource = crystalReport
    HiddenField1.Value = Request.QueryString("ContractNumber")
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 19, 2012 12:43 PM

All replies

  • User1315711699 posted
    Dim crystalReport As New ReportDocument()
    crystalReport.Load(Server.MapPath("FC15ContractPrint.rpt"))
    // here before setting report source set parameter values
    crystalReport.SetParameterValue("ParameterName",value) // paramtere name should be similar to your crystal report parameter

    CrystalReportViewer1.ReportSource = crystalReport
    HiddenField1.Value = Request.QueryString("ContractNumber")
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 19, 2012 12:43 PM
  • User-1721125449 posted
            Dim crystalReport As New ReportDocument()
            crystalReport.Load(Server.MapPath("FC15ContractPrint.rpt"))
            HiddenField1.Value = Request.QueryString("ContractNumber")
    
            crystalReport.SetParameterValue("@ContractNumber", HiddenField1.Value)
            
            CrystalReportViewer1.ReportSource = crystalReport
            Dim crParameterFieldDefinitions As ParameterFieldDefinitions = crystalReport.DataDefinition.ParameterFields()
            Dim crParameterFieldDefinition As ParameterFieldDefinition = crParameterFieldDefinitions.Item("@ContractNumber")
            Dim crParameterValues As New ParameterValues
            Dim crParameterDiscreteValue As New ParameterDiscreteValue() With {.Value = HiddenField1.Value}
            crParameterValues = crParameterFieldDefinition.CurrentValues
            crParameterValues.Add(crParameterDiscreteValue)
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
            CrystalReportViewer1.RefreshReport()

    Now I get an error 'The types of the parameter field and parameter field current values are not compatible. '

    I have NO IDEA what I'm doing at this point. I have yet to find a clear example anywhere of what should be, it seems, an extremely common thing to do.


     

    Thursday, December 20, 2012 3:50 PM
  • User-1721125449 posted

    Hah, just took out all the other stuff that was in there, and it works.

            Dim crystalReport = New ReportDocument()
            crystalReport.Load(Server.MapPath("FC15ContractPrint.rpt"))
            crystalReport.FileName = Server.MapPath("~/FC15ContractPrint.rpt")
            HiddenField1.Value = Request.QueryString("ContractNumber")
            crystalReport.SetParameterValue("@ContractNumber", HiddenField1.Value)
            CrystalReportViewer1.ReportSource = crystalReport

    Thursday, December 20, 2012 4:03 PM