Skip to Content
avatar image
Former Member

Cannot set PrinterName using Visual Studio 2015 in VB.net need suggestions on how to fix.

Hello,

I tried submitting this question yesterday but I can't seem to find it today so sorry if I'm being repetitive.

I have a windows print service that was created with Visual Studio 2008 on Windows Server 2003. I've upgraded the service to now compile and run using Visual Studio 2015 .net framework 4.6.1 on a Windows Server 2012 R2 server.

This server is used to send out print jobs to several different printers that have been add to the print server running the printservice containing this code. A database configuration table is used to determine which printer is to be used based on business rules. So the printer name is pulled from a database table and is applied to the report just prior to calling the Report.PrintToPrinter() method.

The code that used to be used to set the printer name was :

Report.PrintOptions.PrinterName = PrinterName

I've verified that the correct PrinterName is in the PrinterName variable but when this line of code is executed the Report.PrintOptions.PrinterName remains an empty string.

Here's the code I need to fix to get this working: (for some reason it's not formatting correctly)

Private Sub SubmitReport(ByVal EntryId As Long, _ ByVal ReportName As String, _ ByVal PrinterName As String, _ ByRef Report As CrystalDecisions.CrystalReports.Engine.ReportDocument) ' Load the Report Report.Load(ReportName) ' Apply Database Login Parameters For i As Integer = 0 To Report.Database.Tables.Count - 1 'Try Report.Database.Tables(i).SetDataSource(GetDataSource(Report.Database.Tables(i))) Next For Each subReport As CrystalDecisions.CrystalReports.Engine.ReportDocument In Report.Subreports For Each subreportTable As CrystalDecisions.CrystalReports.Engine.Table In subReport.Database.Tables subreportTable.SetDataSource(GetDataSource(subreportTable)) Next Next ' Assign the EntryId to be Printed Report.SetParameterValue("EntryId", EntryId) ' Send Report to Printer ' This will execute the Report and then print it 'Try If Not Me.PrintToFile Then Try FileLog.WriteInfo("SubmitReport: Setting Printer Name (" & EntryId & ")") Report.PrintOptions.PrinterName = PrinterName 'this doesn't work, won't assign the printer's name leaves it blank FileLog.WriteInfo("Saved PrinterName is " & Report.PrintOptions.SavedPrinterName) FileLog.WriteInfo("SubmitReport: Printing EntryId (" & EntryId & ")") Report.PrintToPrinter(1, False, 1, 1) FileLog.WriteInfo("SubmitReport: EntryId (" & EntryId & ") Printed") '_ '' try once again ' When ex.ToString.IndexOf("0x800002AD") > -1 And _ ' ex.ToString.IndexOf("ModifyPrinterName") > -1 Catch ex As Exception _ When ex.ToString.IndexOf("800002AD") > 0 'Dim ExceptionNum As Integer = ex.GetBaseException.Data FileLog.WriteInfo("SubmitReport: Resubmitting Manifest due to Printer Name Error : " _ & "EntryId (" & EntryId & ")" _ & "ReportName (" & ReportName & ")" _ & "PrinterName (" & PrinterName & ")") '& "ExceptionNum (" & ExceptionNum.ToString & ")", _ FileLog.WriteException("SubmitReport: Resubmitting Manifest due to Printer Name Error : " & vbCrLf & ex.ToString, ex) System.Threading.Thread.Sleep(500) Report.PrintOptions.PrinterName = PrinterName Report.PrintToPrinter(1, False, 1, 1) FileLog.WriteInfo("SubmitReport: EntryId (" & EntryId & ") Print Resubmitted") End Try Else Report.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.CrystalReport, "C:\Temp" & ReportName.Substring(ReportName.LastIndexOf("\")) & "_" & (EntryId Mod 56) & ".rpt") End If End Sub

Any suggestions as to a work-around or if there's an existing question that would help me please let me know.

Thank you in advance,

Michael Helduser

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Best Answer
    Apr 21, 2017 at 01:23 PM

    Hi Michael,

    Search for KBA 2163438, it is a complete sample I wrote on printing.

    Don

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Don,

      If I'm attempting to print from a service would your PrintToPrinter button click code be an appropriate set of code to model it after? I just wanted to make sure that this is applicable and that I'm using the correct part of the example you provided. Is the btnPrintToPrinter_click() appropriate for what I'm attempting?

      My original code was performing the following inside of a PrintThread in a windows service:

      1. This is passed as an input parameter, along with the ReportName (local path and filename), PrinterName

      ByRef Report As CrystalDecisions.CrystalReports.Engine.ReportDocument

      2. Load the report

      Report.Load(ReportName)

      3. Database login parameters were then applied

      ' Apply Database Login Parameters

      For i As Integer = 0 To Report.Database.Tables.Count - 1

      'Try

      Report.Database.Tables(i).SetDataSource(GetDataSource(Report.Database.Tables(i)))

      Next

      For Each subReport As CrystalDecisions.CrystalReports.Engine.ReportDocument In Report.Subreports

      For Each subreportTable As CrystalDecisions.CrystalReports.Engine.Table In subReport.Database.Tables

      subreportTable.SetDataSource(GetDataSource(subreportTable))

      Next

      Next

      4. A single parameter value was then applied

      Report.SetParameterValue("EntryId", EntryId)

      5. Then the printer name

      Report.PrintOptions.PrinterName = PrinterName

      6. Then PrintToPrinter was called:

      Report.PrintToPrinter(1, False, 1, 1)

      Thank you very much,

      Michael Helduser