Skip to Content
0

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

Apr 21, 2017 at 12:51 PM

402

avatar image
Former Member

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

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
Don Williams
Apr 21, 2017 at 01:23 PM
0

Hi Michael,

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

Don

Show 6 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Thank you Don. I'll have a look and let you know if I have any follow-up questions. Thank you very much for getting back to me so quickly. Mike.

0
Former Member

Hi Don, I'm waiting on others in my company to provide me with an S User so that I can access the KBA. In the meantime if you could email me the contents of the KBA that would help me to make progress fixing the bug. My email is mbhelduser@ra.rockwell.com. I understand if that's not possible just thought it wouldn't hurt to ask.

Thanks, Michael

0

You should not need a S user account.

See this blog also:

https://blogs.sap.com/2015/08/19/printing-crystal-reports-in-net/

Try thsi link:

https://launchpad.support.sap.com/#/notes/2163438

Don

0
Former Member

Great! I will check that out. Thanks

0
Former Member

Hi Don,

I think the btnPrintToPrinter_click in your example might be the closest match to the scenario I'm dealing with. I'm able to run your code from the KBA in VS2015 and it's identifying the printers so that's a good sign. On Monday I'll try to merge that code in with my print service to see if I can get it working.

I think I'm on the right track but might have some questions for you on Monday.

Thanks again,

Michael Helduser

0
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

0