Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

-------------------------------------------------------
C#
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.

'''
VB.NET
'Class to send string to printer
Public Class RawPrinterHelper
    ' Structure and API declarions:
    <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

    ' SendBytesToPrinter()
    ' When the function is given a printer name and an unmanaged array
    ' of bytes, the function sends those bytes to the print queue.
    ' Returns true on success, false on failure.
    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
        ' Assume failure unless you specifically succeed.
        di.pDocName = "My C#.NET RAW Document"
        di.pDataType = "RAW"

        ' Open the printer.
        If OpenPrinter(szPrinterName.Normalize(), hPrinter, IntPtr.Zero) Then
            ' Start a document.
            If StartDocPrinter(hPrinter, 1, di) Then
                ' Start a page.
                If StartPagePrinter(hPrinter) Then
                    ' Write your bytes.
                    bSuccess = WritePrinter(hPrinter, pBytes, dwCount, dwWritten)
                    EndPagePrinter(hPrinter)
                End If
                EndDocPrinter(hPrinter)
            End If
            ClosePrinter(hPrinter)
        End If
        ' If you did not succeed, GetLastError may give more information
        ' about why not.
        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
        ' Open the file.
        Dim fs As New FileStream(szFileName, FileMode.Open)
        ' Create a BinaryReader on the file.
        Dim br As New BinaryReader(fs)
        ' Dim an array of bytes big enough to hold the file's contents.
        Dim bytes As [Byte]() = New [Byte](fs.Length - 1) {}
        Dim bSuccess As Boolean = False
        ' Your unmanaged pointer.
        Dim pUnmanagedBytes As New IntPtr(0)
        Dim nLength As Integer

        nLength = Convert.ToInt32(fs.Length)
        ' Read the contents of the file into the array.
        bytes = br.ReadBytes(nLength)
        ' Allocate some unmanaged memory for those bytes.
        pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength)
        ' Copy the managed byte array into the unmanaged array.
        Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength)
        ' Send the unmanaged bytes to the printer.
        bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength)
        ' Free the unmanaged memory that you allocated earlier.
        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
        ' How many characters are in the string?
        dwCount = szString.Length
        ' Assume that the printer is expecting ANSI text, and then convert
        ' the string to ANSI text.
        pBytes = Marshal.StringToCoTaskMemAnsi(szString)
        ' Send the converted ANSI string to the printer.
        SendBytesToPrinter(szPrinterName, pBytes, dwCount)
        Marshal.FreeCoTaskMem(pBytes)
        Return True
    End Function
End Class


VB.NET
'Prints receipt
    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

    'Receipt format (Customer copy)
    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

        'returnFlightInfo()

        'Dim sDate As String = ddlDD.SelectedValue.ToString + "/" + ddlMM.SelectedValue.ToString + "/" + ddlYY.SelectedValue.ToString

        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
Posted

I'm afraid you simply don't understand what Web does. You show some VB.NET code you use in ASP.NET. It is written in assumption that you have a Windows system installed on your HTTP server's host computer, and try to communicate with this system. Do you really want to print something on a printer attached to your host computer on a Web farm?
I doubt it? Do you really think a printed is installed on the server side? :-)

I hope my hint makes it clear. What you are trying to do just makes sense. Please don't ask me what to do. I'll answer: learn the basics of programming, Web development, and so on.

—SA
 
Share this answer
 
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

Full error description
 
Share this answer
 
Comments
Member 12111217 12-Jan-16 17:02pm    
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

Add the following section to the configuration file of your application:
<configuration>
<system.web>
<compilation debug="”true”">


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

Full error description
Sergey Alexandrovich Kryukov 12-Jan-16 17:13pm    
Are you talking to yourself? Please remove it. This is not a "solution". Such posts are considered as abuse.
—SA

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