Skip to Content
0
Former Member
Apr 21, 2017 at 12:51 PM

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

692 Views

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