使用vb.net打印表数据

时间:2019-06-28 15:19:06

标签: vb.net exception printing

我正在编写一个触摸屏程序,该程序将车辆登记信息插入mysql数据库,我需要将收据及其详细信息打印给客户。当我尝试打印收据时,我无法识别错误行。当我单击“是”按钮时,出现此“打印机错误。请联系支持”。

Private Sub PrintPageHandler(ByVal sender As Object, ByVal args As Printing.PrintPageEventArgs)
        'Printing function
        Dim sqlConn As MySqlConnection
        Dim cmd As New MySqlCommand
        Dim SQLUser, SQLPassword, strSQL, strID, strSiteName, strSiteID As String

        strSiteID = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\Software\White List Touch Screen\", "siteid", "")
        strSiteName = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\Software\White List Touch Screen\", "sitename", "")
        SQLUser = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\Software\White List Touch Screen\", "sqluser", "")
        SQLPassword = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\Software\White List Touch Screen\", "sqlpwd", "")
        sqlConn = New MySqlConnection("Data Source=localhost;Persist Security Info=True;uid = root; pwd=java; database=exemption")

        strSQL = "SELECT TOP(1) id FROM tblVRM ORDER BY id DESC"
        sqlConn.Open()
        cmd = New MySqlCommand(strSQL, sqlConn)
        strID = cmd.ExecuteScalar
        sqlConn.Close()
        Dim strVRM, FullDate, DateOnly As String
        strVRM = txtVRM.Text
        FullDate = CStr(Date.Now)
        DateOnly = FullDate.Substring(0, 10)

        'Font to use for the variable strVRM
        Dim VRMFont As New Font("Arial", 14)
        Dim ValidForFont As New Font("Arial", 11)

        'For printing each line, the first number is the horizontal alignment, second is for verticle alignment
        args.Graphics.DrawString("WHITE LIST RECEIPT FOR:", New Font(Me.Font, FontStyle.Regular), Brushes.Black, 10, 5)
        args.Graphics.DrawString(strSiteID & ", " & strSiteName, New Font(Me.Font, FontStyle.Bold), Brushes.Black, 10, 20)
        args.Graphics.DrawString("VALID ONLY FOR: " & DateOnly, New Font(ValidForFont, FontStyle.Bold), Brushes.Black, 28, 50)
        args.Graphics.DrawString("FOR VEHICLE REGISTRATION: ", New Font(Me.Font, FontStyle.Regular), Brushes.Black, 10, 80)
        args.Graphics.DrawString(strVRM, New Font(VRMFont, FontStyle.Bold), Brushes.Black, 183, 74)
        args.Graphics.DrawString("RECEIPT No: " & strID, New Font(Me.Font, FontStyle.Bold), Brushes.Black, 10, 95)
        args.Graphics.DrawString("VALID FROM: " & Date.Now, New Font(Me.Font, FontStyle.Bold), Brushes.Black, 10, 110)

    End Sub

    Private Sub btnYes_Click(sender As System.Object, e As System.EventArgs) Handles btnYes.Click
        'Call the print function
        Try
            Dim i As Integer = 0
            Do While i < 2
                Dim Print As New Printing.PrintDocument
                Dim Printer As String = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\Software\White List Touch Screen\", "printer", "")
                Print.PrinterSettings.PrinterName = Printer
                AddHandler Print.PrintPage, AddressOf Me.PrintPageHandler
                Print.Print()
                RemoveHandler Print.PrintPage, AddressOf Me.PrintPageHandler
                pnlResult.Visible = True
                pnlKeyboard.Visible = True
                i = i + 4
            Loop
        Catch
            MessageBox.Show("Printer Errors. Please contact Support")
            Exit Sub
        End Try
    End Sub 

0 个答案:

没有答案