I have made a webpage using receipt printer to print out receipt for customer. It works fine on my computer but it does not work when running it on live.
This is the error description I got when running the webpage.
-------------------------------------------------------
Server Error in ‘/’ Application
Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it orginated in the code.
Exception Details: System.InvalidOperationException: Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application.
Source Error:
The source code that generated this unhandled exception can only be shown when compiled in debug mode. To enable this, please follow one of the below steps, then request the URL:
1. Add a "Debug = true" directive at the top of the file that generated the error. Example:
<%@ Page Language="C#" Debug="true" %>
Or
2. Add the following section to the configuration file of your application:
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
</configuration>
Note that this second technique will cause all files within a given application to be compiled in debug mode. The first technique will cause only that particular file to be compiled in debug mode.
Important: Running application in debug mode does incur a memory/performance overhead. You should make sure that an application has debugging disabled before deploying into production scenario.
Stack Trace:
[InvalidOperationException: Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application.]
System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window owner) +649
MemberPages_Admin_Print_Test.Print_Customer_With_NoPOS() +372
MemberPages_Admin_Print_Test.Button1_Click(Object sender, EventArgs e) +160
System.Web.UI.Webcontrols.Button.Onclick(EventArgs e) +11753681
System.Web.UI.Webcontrols.Button.RaisePostBackEvent(String eventArgument) +150
System.Web.UI,Page,ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +6016
-----------------------------------------------------
And this is the code of printing out receipt.
'''
Public Class RawPrinterHelper
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
Public Class DOCINFOA
<MarshalAs(UnmanagedType.LPStr)>
Public pDocName As String
<MarshalAs(UnmanagedType.LPStr)>
Public pOutputFile As String
<MarshalAs(UnmanagedType.LPStr)>
Public pDataType As String
End Class
<DllImport("winspool.Drv", EntryPoint:="OpenPrinterA", SetLastError:=True, CharSet:=CharSet.Ansi, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function OpenPrinter(<MarshalAs(UnmanagedType.LPStr)> szPrinter As String, ByRef hPrinter As IntPtr, pd As IntPtr) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="ClosePrinter", SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function ClosePrinter(hPrinter As IntPtr) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="StartDocPrinterA", SetLastError:=True, CharSet:=CharSet.Ansi, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function StartDocPrinter(hPrinter As IntPtr, level As Int32, <[In], MarshalAs(UnmanagedType.LPStruct)> di As DOCINFOA) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="EndDocPrinter", SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function EndDocPrinter(hPrinter As IntPtr) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="StartPagePrinter", SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function StartPagePrinter(hPrinter As IntPtr) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="EndPagePrinter", SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function EndPagePrinter(hPrinter As IntPtr) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="WritePrinter", SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function WritePrinter(hPrinter As IntPtr, pBytes As IntPtr, dwCount As Int32, ByRef dwWritten As Int32) As Boolean
End Function
Public Shared Function SendBytesToPrinter(szPrinterName As String, pBytes As IntPtr, dwCount As Int32) As Boolean
Dim dwError As Int32 = 0, dwWritten As Int32 = 0
Dim hPrinter As New IntPtr(0)
Dim di As New DOCINFOA()
Dim bSuccess As Boolean = False
di.pDocName = "My C#.NET RAW Document"
di.pDataType = "RAW"
If OpenPrinter(szPrinterName.Normalize(), hPrinter, IntPtr.Zero) Then
If StartDocPrinter(hPrinter, 1, di) Then
If StartPagePrinter(hPrinter) Then
bSuccess = WritePrinter(hPrinter, pBytes, dwCount, dwWritten)
EndPagePrinter(hPrinter)
End If
EndDocPrinter(hPrinter)
End If
ClosePrinter(hPrinter)
End If
If bSuccess = False Then
dwError = Marshal.GetLastWin32Error()
End If
Return bSuccess
End Function
Public Shared Function SendFileToPrinter(szPrinterName As String, szFileName As String) As Boolean
Dim fs As New FileStream(szFileName, FileMode.Open)
Dim br As New BinaryReader(fs)
Dim bytes As [Byte]() = New [Byte](fs.Length - 1) {}
Dim bSuccess As Boolean = False
Dim pUnmanagedBytes As New IntPtr(0)
Dim nLength As Integer
nLength = Convert.ToInt32(fs.Length)
bytes = br.ReadBytes(nLength)
pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength)
Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength)
bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength)
Marshal.FreeCoTaskMem(pUnmanagedBytes)
Return bSuccess
End Function
Public Shared Function SendStringToPrinter(szPrinterName As String, szString As String) As Boolean
Dim pBytes As IntPtr
Dim dwCount As Int32
dwCount = szString.Length
pBytes = Marshal.StringToCoTaskMemAnsi(szString)
SendBytesToPrinter(szPrinterName, pBytes, dwCount)
Marshal.FreeCoTaskMem(pBytes)
Return True
End Function
End Class
Public Sub print_Customer()
Dim pd As New PrintDialog
Dim pdoc As New PrintDocument
AddHandler pdoc.PrintPage, AddressOf pdoc_PrintPage_Customer
Dim ps As New PrinterSettings
Dim font As New Font("Courier New", 15)
Dim psize As New PaperSize("Custom", 100, 200)
pd.Document = pdoc
pd.Document.DefaultPageSettings.PaperSize = psize
pdoc.DefaultPageSettings.PaperSize.Height = 820
pdoc.DefaultPageSettings.PaperSize.Width = 520
Dim result As DialogResult = DialogResult.Yes
pdoc.PrinterSettings.PrinterName = "EPSON TM-88"
pdoc.Print()
Dim gs As String = Convert.ToString(ChrW(29))
Dim esc As String = Convert.ToString(ChrW(27))
Dim command As String = ""
command = esc & "@"
command += gs & "V" & ChrW(1)
RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, command)
End Sub
Private Sub pdoc_PrintPage_Customer(sender As Object, e As PrintPageEventArgs)
Dim graphics As Graphics = e.Graphics
Dim font As New Font("Courier New", 10)
Dim fontheight As Single = font.GetHeight
Dim startX As Integer = 50
Dim startY As Integer = 10
Dim Offset As Integer = 20
Dim underline As [String] = "----------------------------------"
Dim cutline As [String] = "**********************************"
graphics.DrawString(cutline, New Font("Courier New", 8), New SolidBrush(Color.Black), startX, startY + Offset)
Offset = Offset + 30
End Sub
'''
I can see that modal dialog does not work on server to print out receipt. Please help me out to find other way to run this webpage properly.
Thank you