Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > Crystal Reports
|
Crystal Reports General discussion about Crystal Reports. For discussions specific to the book Professional Crystal Reports for VS.NET, please see the book discussion forum for that book.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Crystal Reports section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 28th, 2006, 08:59 AM
Registered User
 
Join Date: Mar 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using Crystal in VB 2005 without Report Viewer

I am writing a Windows application with Visual Basic 2005. Within my application, I need to print a Crystal Report and pass it a few parameters. I have searched the online help, and it only shows how to do this with the Report Viewer. This is a step I need to bypass. I want to automate the printing process, and not show the user the report viewer, or even load it due to extra resources that I do not need.

I need to know how to pass parameters to the ReportDocument object programatically. So far, I have had no luck finding this anywhere online. I need your help!

Clif Dunaway
 
Old March 29th, 2006, 12:46 AM
Authorized User
 
Join Date: Sep 2005
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to vijaykumartaduri Send a message via Yahoo to vijaykumartaduri
Default

Clif,

Try with below code

Public Sub PrintReport(Byval ReportName As CrystalDecisions.CrystalReports.Engine.ReportDocum ent, ByVal ParamName() As String, ByVal ParamValues() As String )

        Dim MyReportDocument As CrystalDecisions.CrystalReports.Engine.ReportDocum ent = ReportName
        Dim Field As CrystalDecisions.Shared.ParameterValues
        Dim Value As CrystalDecisions.Shared.ParameterDiscreteValue

        Try

            If Not ParamName Is Nothing Then
                For i As Integer = 0 To ParamName.Length - 1
                    Field = New CrystalDecisions.Shared.ParameterValues
                    Value = New CrystalDecisions.Shared.ParameterDiscreteValue
                    Value.Value = ParamValues(i)
                    Field.Add(Value)
                    MyReportDocument.DataDefinition.ParameterFields(Pa ramName(i)).ApplyCurrentValues(Field)
                Next
            End If

            MyReportDocument.PrintOptions.PrinterName = ""
            MyReportDocument.PrintToPrinter(1, True, 0, 0)

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OKOnly, "Print Report")
        Finally
            MyReportDocument.Close()
            MyReportDocument.Dispose()
        End Try
    End Sub



    example:

     Private Sub PrintReport()
        Dim strParamSelect As String
        Dim strParamName() As String = {"xxx", "yyyy"}
        Dim strParamValues() As String = {"Value of xxx", "Value of yyyy"}
        PrintReport(New REPORTNAME, strParamName, strParamValues)
    End Sub

 
Old March 29th, 2006, 09:41 AM
Registered User
 
Join Date: Mar 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Awesome! Many thanks! I was able to get this working, with a tweak to load a report. To call the print routine I did this:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim strParamName() As String = {"@PalletID"}
        Dim strParamValues() As String = {"J00002229401"}

        Dim REPORTNAME As New CrystalDecisions.CrystalReports.Engine.ReportDocum ent

        REPORTNAME.Load("C:\Reports\Report1.rpt")
        PrintReport(REPORTNAME, strParamName, strParamValues)

    End Sub

Clif Dunaway
 
Old April 3rd, 2006, 01:12 PM
Registered User
 
Join Date: Mar 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am now getting a connection error when I call my report to print. Crystal is saying:
Unable to connect: incorrect log on parameters.

I added this line:
MyReportDocument.SetDatabaseLogon("username", "password", "server", "database")
with real data, and it still has this error.

Does anyone know why this error is appearing? How can I get past this?


Clif Dunaway
 
Old April 3rd, 2006, 02:46 PM
Authorized User
 
Join Date: Sep 2005
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to vijaykumartaduri Send a message via Yahoo to vijaykumartaduri
Default

Before loading the report you need to pass report databse login details .

 
Old April 4th, 2006, 01:09 AM
Authorized User
 
Join Date: Sep 2005
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to vijaykumartaduri Send a message via Yahoo to vijaykumartaduri
Default

Clif,

  Try with this below code...........

  Private Sub LogOnReport(ByVal cr As CrystalDecisions.CrystalReports.Engine.ReportDocum ent)
        Dim myTable As CrystalDecisions.CrystalReports.Engine.Table
        Dim myLogin As CrystalDecisions.Shared.TableLogOnInfo
        For Each myTable In cr.Database.Tables
            myLogin = myTable.LogOnInfo
            myLogin.ConnectionInfo.ServerName ="ServerName"
            myLogin.ConnectionInfo.DatabaseName = "DBNAme"
            myLogin.ConnectionInfo.UserID = "USERNAME"
            myLogin.ConnectionInfo.Password = "Password"
            myTable.ApplyLogOnInfo(myLogin)
            'Use this below line to set report location
            If Not myTable.Name Like "Command*" Then myTable.Location = strInfo(1) & ".dbo." & myTable.Name
        Next

      ' This below code is for subreport login

        Dim RepObject As CrystalDecisions.CrystalReports.Engine.ReportObjec t
        Dim SubRepDoc As CrystalDecisions.CrystalReports.Engine.SubreportOb ject
        Dim RepDoc As CrystalDecisions.CrystalReports.Engine.ReportDocum ent
        For Each RepObject In cr.ReportDefinition.ReportObjects
            If RepObject.Kind = CrystalDecisions.Shared.ReportObjectKind.Subreport Object Then
                SubRepDoc = CType(RepObject, CrystalDecisions.CrystalReports.Engine.SubreportOb ject)
                RepDoc = cr.OpenSubreport(SubRepDoc.SubreportName)
                For Each myTable In RepDoc.Database.Tables
                    myLogin = myTable.LogOnInfo
                    myLogin.ConnectionInfo.ServerName = strInfo(0)
                    myLogin.ConnectionInfo.DatabaseName = strInfo(1)
                    myLogin.ConnectionInfo.UserID = strInfo(2)
                    myLogin.ConnectionInfo.Password = strInfo(3)
                    myTable.ApplyLogOnInfo(myLogin)
                    If Not myTable.Name Like "Command*" Then myTable.Location = strInfo(1) & ".dbo." & myTable.Name
                Next
            End If
        Next

    End Sub

 
Old July 18th, 2007, 11:48 AM
Registered User
 
Join Date: Jul 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have modified this to work for exporting to text and was wondering if anyone has had any luck in doing so as well. I cannot change the default export to text options. The characters per inch field needs to be changed from the default 12 to 16 and the Lines per page to 10.
I keep on getting errors when doing so and it just does not work for me. This is the code that I have modified from above.
Code:
            Dim exportOpts As New ExportOptions
            Dim diskOpts As DiskFileDestinationOptions
            diskOpts = ExportOptions.CreateDiskFileDestinationOptions()

            CrystalDecisions.Shared.ExportOptions.CreateTextFormatOptions.CharactersPerInch = 16
            exportOpts.ExportFormatType = ExportFormatType.Text

            exportOpts.ExportDestinationType = ExportDestinationType.DiskFile

            diskOpts.DiskFileName = "c:\exporttotext.txt"
            exportOpts.ExportDestinationOptions = diskOpts

            ExportOptions.CreateTextFormatOptions.CharactersPerInch = 16   ' This line seems to do nothing but should do something
            ExportOptions.CreateTextFormatOptions.LinesPerPage = 5 'Does nothing but should
            MyReportDocument.Export(exportOpts)

Thanks for your help






Similar Threads
Thread Thread Starter Forum Replies Last Post
Deploye crystal report with VB.NET 2005 zrtv Crystal Reports 2 June 6th, 2009 08:09 AM
VB 2005 Crystal Report Connection with SQL 2000 blackspear BOOK: Professional Crystal Reports for VS.NET 0 December 3rd, 2008 09:49 AM
Vb.net with Crystal Report Viewer sgupta .NET Framework 1.x 0 November 27th, 2006 02:39 PM
Crystal Report (9) Viewer Problem Kaustav VB Components 1 October 30th, 2005 07:02 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.