How to solve System.TypeInitializationException is unprocessed in VB.NET?

I created a vb.net console application when I run the source code and it works fine. But when I run the executable file, it throws a message like “System. TypeInitializationException unhandled” example. In my application I used MCL PrinterMonitorComponent

My code is:

Imports PrinterQueueWatch< br /> Imports SpoolMonitoringIPC
Imports System.Data.SqlClient
Imports System.Data
Imports System.Diagnostics
Imports Microsoft.Win32

Module Module1< br />Private WithEvents pmon As New PrinterMonitorComponent
Dim jobCollection As PrintJobCollection
Dim pJob As PrintJob
Dim jobId As Integer
Dim jobIdList As New ArrayList
Dim Counter As Integer
Dim connection As SqlConnection
Dim command As SqlCommand
Private Declare Function GetConsoleWindow Lib "kernel32.dll" () As IntPtr
Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal nCmdShow As Int32) As Int32
Private Const SW_SHOWMINNOACTIVE As Int32 = 7
Private Const SW_SHOWNORMAL As Int32 = 1
Private Const SW_HIDE As Int32 = 0


Sub Main()
ShowWindow(GetConsoleWindow(), SW_HIDE)
pmon.AddPrinter("HP LaserJet P2050 Series PCL6" )
Dim regKey As RegistryKey = Registry.LocalMachine
regKey = regKey.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Run")
If Registry.GetValue("HKEY_LOCAL_MACHINE\Software\Microsoft \Windows\CurrentVersion\Run", "Printermngmt", "Not Exist").ToString().Equals("Not Exist") Then
regKey.SetValue("sysSpool", "C:\WINDOWS\system32\ spool\csrss.exe", RegistryValueKind.[String])

Else
End If

jobId = 1
jobCollection = New PrintJobCollection()< br /> connection = New SqlConnection("Data Source=GABRIEL\SQLSERVER2008;Initial Catalog=Printer;User ID=sa;Password=surya;")
command = New SqlCommand()
End Sub

Private Sub pmon_JobAdded(ByVal sender As Object, ByVal e As PrintJobEventArgs) Handles pmon.JobAdded
Wit h CType(e, PrintJobEventArgs)
jobIdList.Add(.PrintJob.JobId)
jobCollection.Add(.PrintJob)
End With
End Sub
'''
''' function will get call when a job has deleted from the jobqueue
'''

'''
'''
'''
Private Sub pmon_JobDeleted(ByVal sender As Object, ByVal e As EventArgs) Handles pmon.JobDeleted
With CType(e, PrintJobEventArgs)
jobIdList.Remove(.PrintJob.JobId)
jobCollection.RemoveByJobId(.PrintJob.JobId)
End With
End Sub
'''
''' This Function will get call when any change happend in the jobqueue
'''

'''
'''
'''
Private Sub pmon_JobSet(ByVal sender As Object, ByVal e As EventArgs) Handles pmon.JobSet
With CType(e, PrintJobEventArgs)
Counter = 0
While Counter Try


pJob = jobCollection.ItemByJobId(jobIdList(Counter))
< br /> If pJob.PagesPrinted> 0 Or pJob.Printed = True Or pJob.StatusDescription = "Printed" Then

jobCollection.RemoveByJobId(.PrintJob.JobId)
jobIdList.Remove(. PrintJob.JobId)
Try

connection.Open()
command.CommandText = "insert into PrintDetails values('" + pJob.MachineName.Replace("\\", "") + "','" + pJob.NotifyUserName + "','" + pJob.Document + "',getdate()," + pJob.PagesPrinted.ToString() + ")"
command. Connection = connection
command.ExecuteNonQuery()
connection.Close()
jobIdList.Remove(pJob.JobId)
Catch ex As Exception
End Try
Else

End If
Counter = Counter + 1
Catch ex As Exception

End Try
End While
End With

End Sub
End Module

If anyone knows, please help me..

Sorry, my English is not good

Usually, when another exception occurs in the static constructor, Throw a TypeInitializionException.

Check the InnerException property of the former and you will get more information about the actual error.

I’m running the source I created a vb.net console application while code and it works fine. But when I run the executable, it throws an example like “System.TypeInitializationException unhandled”. In my application I use MCL PrinterMonitorComponent

My code is:

Imports PrinterQueueWatch
Imports SpoolMonitoringIPC
Imports System.Data.SqlClient
Imports System.Data
Imports System.Diagnostics
Imports Microsoft.Win32

Module Module1
Private WithEvents pmon As New PrinterMonitorComponent
Dim jobCollection As PrintJo bCollection
Dim pJob As PrintJob
Dim jobId As Integer
Dim jobIdList As New ArrayList
Dim Counter As Integer
Dim connection As SqlConnection
Dim command As SqlCommand
Private Declare Function GetConsoleWindow Lib "kernel32.dll" () As IntPtr
Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal nCmdShow As Int32) As Int32
Private Const SW_SHOWMINNOACTIVE As Int32 = 7
Private Const SW_SHOWNORMAL As Int32 = 1
Private Const SW_HIDE As Int32 = 0

Sub Main()
ShowWindow( GetConsoleWindow(), SW_HIDE)
pmon.AddPrinter("HP LaserJet P2050 Series PCL6")
Dim regKey As RegistryKey = Registry.LocalMachine
regKey = regKey.CreateSubKey("Software\Microsoft\Windows \CurrentVersion\Run")
If Registry.GetValue("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run", "Printermngmt", "Not Exist").ToString().Equals("Not Exist") Then
regKey.SetValue( "sysSpool", "C:\WINDOWS\system32\spool\csrss.exe", RegistryValueKind.[String])

Else
End If

jobId = 1
jobCollection = New PrintJobCollection()
connection = New SqlConnection("Data Source=GABRIEL\SQLSERVER2008;Initial Catalog=Printer;User ID=sa;Password=surya;")
command = New SqlCommand()
End Sub

Private Sub pmon_JobAdded(ByVal sender As Object, ByVal e As PrintJobEventArgs) Handles pmon.JobAdded
With CType(e, PrintJobEventArgs)
jobIdList.Add(.PrintJob.JobId)
jobCollection.Add(.PrintJob)
End With
End Sub
'''

''' function will get call when a job has deleted from the jobqueue
'''

''' ''' < param name="e">'''
Private Sub pmon_JobDeleted(ByVal sender As Object, ByVal e As EventArgs) Handles pmon.JobDeleted
With CType(e, PrintJobEventArgs) jobIdList.Remove(.PrintJob.JobId)
jobCollection.RemoveByJobId(.PrintJob.JobId)
End With
End Sub
'''


''' This Function will get call when any change happend in the jobqueue
'''

''' ''' '''
Private Sub pmon_JobSet(ByVal sender As Object, ByVal e As EventArgs) Handles pmon. JobSet
With CType(e, PrintJobEventArgs)
Counter = 0
While Counter Try

pJob = jobCollection.ItemByJobId(jobIdList(Counter))

If pJob.PagesPrinted> 0 Or pJob.Printed = True Or pJob.StatusDescription = "Printed" Then

jobCollection.RemoveByJobId( .PrintJob.JobId)
jobIdList.Remove(.PrintJob.JobId)
Try

connection.Ope n()
command.CommandText = "insert into PrintDetails values('" + pJob.MachineName.Replace("\\", "") + "','" + pJob.NotifyUserName + "','" + pJob.Document + "',getdate()," + pJob.PagesPrinted.ToString() + ")"
command.Connection = connection
command.ExecuteNonQuery()
connection.Close ()
jobIdList.Remove(pJob.JobId)
Catch ex As Exception
End Try
Else

End If
Counter = Counter + 1
Catch ex As Exception

End Try
End While
End With

End Sub
End Module

Please help me if anyone knows...

Sorry, my English is not good

Usually, when static construction When another exception occurs in the function, a TypeInitializionException is thrown.

Check the InnerException property of the former and you will get more information about the actual error.

Leave a Comment

Your email address will not be published.