Results 1 to 4 of 4

Thread: [RESOLVED] Load Report Failed - Crystal Reports

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2012
    Location
    Minnesota
    Posts
    238

    Resolved [RESOLVED] Load Report Failed - Crystal Reports

    I have a vs2010 winforms app with sql 2008 r2 backend. I am trying to run a crystal report from within my app. I have a form with the CrystalReportViewer on it and I have a form with a button running the code to open the viewer and load the report.

    I get the "Load Report Failed" error. The report takes a parameter and I have a textbox on the crystalreportviewer form to allow the user to enter a value or change the value, but it crashes on the load and doesn't even get to that code. If I choose the report from the smart tag on the CrystalReportViewer the report will load and prompt for the paramater value. I cannot figure out how to be able to programmically load the report and then have user enter parameter.

    Here is the code I use to load the report.
    Code:
            Dim zcrystalviewer As New CrystalReportViewer
            zcrystalviewer.Show()
            Dim cryRpt As New ReportDocument
            cryRpt.Load("C:\Member Program\Member Program\Member Program\Reports\statelist.rpt")
    
            Dim crParameterFieldDefinitions As ParameterFieldDefinitions
            Dim crParameterFieldDefinition As ParameterFieldDefinition
            Dim crParameterValues As New ParameterValues
            Dim crParameterDiscreteValue As New ParameterDiscreteValue
    
            crParameterDiscreteValue.Value = TextBox1.Text
            crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields
            crParameterFieldDefinition = crParameterFieldDefinitions.Item("zState")
            crParameterValues = crParameterFieldDefinition.CurrentValues
    
            crParameterValues.Clear()
            crParameterValues.Add(crParameterDiscreteValue)
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
    
            zcrystalviewer.CrystalReportViewer1.ReportSource = cryRpt
            zcrystalviewer.CrystalReportViewer1.Refresh()
    It crashes on this line of code: cryRpt.Load("C:\Member Program\Member Program\Member Program\Reports\statelist.rpt").

    I have this same report without the parameter and it loads (cryRpt.Load("C:\Member Program\Member Program\Member Program\Reports\worldir.rpt")) just perfectly.

    What am I doing wrong? Can I load the report first before getting the parameter?

    Thanks!
    Stacy

    P.S. I did a tutorial and this is exactly how they did it in the tutorial and that one works. It had one parameter and they could change it in the textbox and reload the report with the new parameter.

  2. #2
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: Load Report Failed - Crystal Reports

    Maybe parts of the next code & comments could help...
    VB.NET and CRYSTAL REPORTS
    HOW TO CALL A RPT WITH SUBREPORTS AND PARAMETERS
    -------------------------------------------------
    The following code shows how to load Crystal Reports in VB.NET, solving all the issues of logon, including sub reports and parameter passing.
    You can view your reports by simply calling the required functions with its parameters.

    Using this code in your application is very simple.
    In the first step you need to add a form, and name it frmViewReport, then place the Crystal Report Viewer control on the form and name it rptViewer.
    In the code section, simply paste the following function.
    You can call this function from anywhere in your application with reference to the frmViewReport form.
    The sample code for calling the function is given below:
    Dim objForm As New frmViewReport
    objForm.ViewReport("C:\test.rtp", , "@parameter1=testĀ¶mter2=10")
    objForm.show()
    Now let me explain you in detail what is going on in the code and what the format of the parameter string is.
    If there are parameters in the Crystal Reports then they should be passed with their values to the param of the function.
    The parameter string should be in the following format:
    <First Parameter Name>=<First Paramter Value>&
    <Second Parameter Name>=<Second Paramter Value>..
    Note the parameter name and its value pairs are separated by an '&'.
    The report name with its full path should be passed to sReportName function.

    Following is the function code with comments.
    Code:
    Friend Function ViewReport(ByVal sReportName As String, Optional ByVal sSelectionFormula As String = "", Optional ByVal param As String = "") As Boolean
    
        'Declaring variables
        Dim intCounter   As Integer
        Dim intCounter1  As Integer
        Dim strTableName As String
        Dim objReportsParameters As frmReportsParameters
    
        'Crystal Report's report document object
        Dim objReport As New rystalDecisions.CrystalReports.Engine.ReportDocument
    
        Dim mySection  As CrystalDecisions.CrystalReports.Engine.Section
        Dim mySections As CrystalDecisions.CrystalReports.Engine.Sections
    
        'object of table Log on info of Crystal report
        Dim ConInfo As New CrystalDecisions.Shared.TableLogOnInfo
    
        'Parameter value object of crystal report 
        ' parameters used for adding the value to parameter.
        Dim paraValue As New CrystalDecisions.Shared.ParameterDiscreteValue
    
        'Current parameter value object(collection) of crystal report parameters.
        Dim currValue As CrystalDecisions.Shared.ParameterValues
    
        'Sub report object of crystal report.
        Dim SubRptO As CrystalDecisions.CrystalReports.Engine.SubreportObject
    
        'Sub report document of crystal report.
        Dim SubRptD As New CrystalDecisions.CrystalReports.Engine.ReportDocument
        
        Dim strParamenters As String
        Dim strParValPair() As String
        Dim strVal() As String
        Dim sFileName As String
        Dim index As Integer
    
        Try
    
            'Load the report
            sFileName = DownloadReport(sReportName, m_strReportDir)
            objReport.Load(sFileName)
    
            'Check if there are parameters or not in report.
            'As parameter fields collection also picks the selection formula 
            ' which is not the parameter so if total parameter count is 1 
            ' then we check whether its a parameter or selection formula.
            intCounter = objReport.DataDefinition.ParameterFields.Count
            If intCounter = 1 Then
                If InStr(objReport.DataDefinition.ParameterFields(0).ParameterFieldName, ".", CompareMethod.Text) > 0 Then
                    intCounter = 0
                End If
            End If
    
            'If there are parameters in report and user has passed them then 
            ' split the parameter string and Apply the values to their concurrent parameters.
            If intCounter > 0 And Trim(param) <> "" Then
                strParValPair = strParamenters.Split("&")
                For index = 0 To UBound(strParValPair)
                    If InStr(strParValPair(index), "=") > 0 Then
                        strVal = strParValPair(index).Split("=")
                        paraValue.Value = strVal(1)
                        currValue = objReport.DataDefinition.ParameterFields(strVal(0)).CurrentValues
                        currValue.Add(paraValue)
                            objReport.DataDefinition.ParameterFields(strVal(0)).ApplyCurrentValues(currValue)
                    End If
                Next
            End If
    
           'Set the connection information to ConInfo object so that we can apply the 
           ' connection information on each table in the report
            ConInfo.ConnectionInfo.UserID = objDataBase.UserName
            ConInfo.ConnectionInfo.Password = objDataBase.Password
            ConInfo.ConnectionInfo.ServerName = objDataBase.Server
            ConInfo.ConnectionInfo.DatabaseName = objDataBase.Database
    
            For intCounter = 0 To objReport.Database.Tables.Count - 1
                objReport.Database.Tables(intCounter).ApplyLogOnInfo(ConInfo)
            Next
    
            'Loop through each section on the report then look through each object
            ' in the section if the object is a subreport, then apply logon info 
            ' on each table of that sub report
            For index = 0 To objReport.ReportDefinition.Sections.Count - 1
                For intCounter = 0 To objReport.ReportDefinition.Sections(index).ReportObjects.Count - 1
                    With objReport.ReportDefinition.Sections(index)
                        If .ReportObjects(intCounter).Kind = CrystalDecisions.Shared.ReportObjectKind.SubreportObject Then
                            SubRptO = CType(.ReportObjects(intCounter), CrystalDecisions.CrystalReports.Engine.SubreportObject)
                            SubRptD = SubRptO.OpenSubreport(SubRptO.SubreportName)
                            For intCounter1 = 0 To SubRptD.Database.Tables.Count - 1
                                SubRptD.Database.Tables(intCounter1).ApplyLogOnInfo(ConInfo)
                            Next
    		        If sSelectionFormula.Length > 0 Then
    		            SubRptO.RecordSelectionFormula = sSelectionFormula
    		        End If
                        End If
                    End With
                Next
            Next
    
           'If there is a selection formula passed to this function then use that
            If sSelectionFormula.Length > 0 Then
                objReport.RecordSelectionFormula = sSelectionFormula
            End If
    
            'Re setting control 
            rptViewer.ReportSource = Nothing
    
            'Set the current report object to report.
            rptViewer.ReportSource = objReport
    
            'Show the report
            rptViewer.Show()
    
            Application.DoEvents()
    
            Me.Text = sReportName
            MyBase.Visible = True
            Me.BringToFront()
    
            Return True
    
            Catch ex As System.Exception
                MsgBox(ex.Message)
        End Try
    
    End Function
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2012
    Location
    Minnesota
    Posts
    238

    Re: Load Report Failed - Crystal Reports

    Thanks jggtz for your reply.

    I can't seem to get that code to work.

    -Stacy

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2012
    Location
    Minnesota
    Posts
    238

    Re: Load Report Failed - Crystal Reports

    OK I took out the parameter and the report loads just fine using the following code:
    Code:
         Dim zcrystalviewer As New CrystalReportViewer
            zcrystalviewer.Show()
            Dim cryRpt As New ReportDocument
            cryRpt.Load("C:\Member Program\Member Program\Member Program\Reports\statelst.rpt")
            zcrystalviewer.CrystalReportViewer1.ReportSource = cryRpt
            zcrystalviewer.CrystalReportViewer1.Refresh()
    As soon as I add back the parameter I get the "Load Report Failed" on the cryRpt.Load("C:\Member Program\Member Program\Member Program\Reports\statelst.rpt") line of code in my button.

    Does anyone know what I am doing wrong. I've been searching the internet for tutorials and information on VS2010 & Crystal Reports with parameters and I still can't find what the problem is. Does anyone know the proper way to send a parameter to Crystal Report loading in a CrystalReportViewer from winforms app using vb.net?

    Thanks,
    Stacy
    Last edited by StacyOW; Feb 6th, 2014 at 09:05 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width