Click here to Skip to main content
15,558,870 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I tried this code, but it does not print. How can I solve this?

What I have tried:

Imports MySql.Data.MySqlClient
Imports System.IO
Imports System.Globalization
Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine

Public Sub Print_Tic()

            ''call the report.
            Dim crConnectionInfo As New ConnectionInfo
            Dim crtableLogoninfos As New TableLogOnInfos
            Dim crtableLogoninfo As New TableLogOnInfo
            Dim CrTables As Tables
            Dim CrTable As Table
            Dim cryRpt As New ReportDocument
            cryRpt.Load(Application.StartupPath + "\" + "tic.rpt")

            With crConnectionInfo
                .ServerName = "localhost"
                .DatabaseName = "opd"
                .UserID = "root"
                .Password = ""
                .IntegratedSecurity = False
            End With

            CrTables = cryRpt.Database.Tables
            For Each CrTable In CrTables
                crtableLogoninfo = CrTable.LogOnInfo
                crtableLogoninfo.ConnectionInfo = crConnectionInfo

            cryRpt.SetDatabaseLogon("root", crConnectionInfo.Password, crConnectionInfo.ServerName, crConnectionInfo.DatabaseName, crConnectionInfo.IntegratedSecurity)

            ''add parameters 
            cryRpt.SetParameterValue(0, txtOPD.Text)
            cryRpt.PrintOptions.PrinterName = "EPSON L360 Series" ''specify the printer name here, if none is specified, the default printer will bee used.
            cryRpt.PrintToPrinter(1, False, 0, 0)
        Catch ex As Exception

        End Try
    End Sub

 Private Sub cmdSave_Click(sender As Object, e As EventArgs) Handles cmdSave.Click
        Call Save()
    End Sub
Updated 2-Feb-22 3:43am

1 solution

Issue seems with PrinterName, Give the proper network path of PrinterName or Write the following code to get your default printer:
cryRpt.PrintOptions.PrinterName = GetDefaultPrinter()

Private Function GetDefaultPrinter() As String
    Dim settings As PrinterSettings = New PrinterSettings()
    For Each printer As String In PrinterSettings.InstalledPrinters
        settings.PrinterName = printer
        If settings.IsDefaultPrinter Then
            Return printer
        End If
    Return String.Empty
End Function
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900