Results 1 to 9 of 9

Thread: Run crestal report whith sub report in vb.net 2005

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2007
    Location
    Egypt
    Posts
    29

    Run crestal report whith sub report in vb.net 2005

    i use crestal report xi & vb.net & database ms sql server 2000

    when i need to run the report from vb.net i use this code


    Code:
     Sub vrep(ByVal vsql As String, ByVal vname As String)
    
            Cn.Execute("Drop View " & vname)
            Cn.Execute("Create View " & vname & " as " & vsql)
    
            Dim sql As String = vsql
            Dim da As New SqlClient.SqlDataAdapter(sql, Col)
    
            Dim ds As New DataSet
            Dim FrmCrystalReport As New FrmPrivew
    
             Dim CrystalReport As New report filse
    
            ds.Clear()
            da.Fill(ds, vname)
            CrystalReport.Database.Tables(0).SetDataSource(ds)
            
            FrmCrystalReport.CrystalReportViewer1.ReportSource = CrystalReport
            FrmCrystalReport.Show()
    
        End Sub
    and its ok

    but now i make report contains a subreport plz help me to Modified the code
    Because the report in this case contains two reports and two queries

    And when i used the previous code does not update the data in the subreport

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

    Re: Run crestal report whith sub report in vb.net 2005

    The next is a general sample, you could check just the subreports part...

    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:
    Code:
    	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
                        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

  3. #3
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Run crestal report whith sub report in vb.net 2005

    I ran across this searching the forum for something in a CR I'm working on. Other than a typo and a couple missing declarations (VB 2008) it is an excellent example. Kudos to jggtz. We will be using it here at our shop. Thanks! jggtz

  4. #4
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Kolkata, India
    Posts
    290

    Re: Run crestal report whith sub report in vb.net 2005

    How can i pass the numeric value as a parameter

  5. #5
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Run crestal report whith sub report in vb.net 2005

    Quote Originally Posted by asm View Post
    How can i pass the numeric value as a parameter
    Using this example I pass numerics without issues. Is it failing somehow?

  6. #6
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Kolkata, India
    Posts
    290

    Re: Run crestal report whith sub report in vb.net 2005

    I have made a report using sql store procedure directly where procedure requires three parameter .. one is numeric and two are string.
    In CR it show correctly but when i call it in vb 2005 using same type of function for calling report... CR want parameter value?
    I do't understand why?

  7. #7
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Run crestal report whith sub report in vb.net 2005

    It sounds like you are not actually overriding the parameters in the CR from VB and CR is asking for them. Is the connection in the CR using a DSN?

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

    Re: Run crestal report whith sub report in vb.net 2005

    The above sample show how to pass parameters to a CR report that has parameters
    In your case you need to pass parameters values to the Stored Procedure in order to execute it and after that pass the returned dataset to CR

    The next link show a sample, follow it --> http://www.macronimous.com/resources...and_VB.NET.asp
    or google it --> http://www.google.com.mx/search?hl=e...meta=&aq=f&oq=

  9. #9
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Kolkata, India
    Posts
    290

    Re: Run crestal report whith sub report in vb.net 2005

    not using dsn connection

Tags for this Thread

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