how to solve System.TypeInitializationException was unhandled exception in vb.net?
I have created a vb.net console application when i am running the source code it will work fine.But when i am running the Executable file it is throwing an exeption like "System.TypeInitializationException was unhandled".In my application i have used 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 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()
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
''' <summary>
''' function will get call when a job has deleted from the jobqueue
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
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
''' <summary>
''' This Function will get call when any change happend in the jobqueue
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub pmon_JobSet(ByVal sender As Object, ByVal e As EventArgs) Handles pmon.JobSet
With CType(e, PrintJobEventArg开发者_如何学Gos)
Counter = 0
While Counter < jobIdList.Count()
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.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 any one know please help me..
sorry for my bad english
Generally, a TypeInitializionException
is thrown when another exception happens inside a static constructor.
Check for the InnerException
property of the former, and you will get more information about the actual error.
When I encountered this issue, the exception pointed to a line of code that was actually not related to the configuration item that was missing from the Web.Config file. Basically I was using a Constants.cs file to initialise settings from the Web.config file that were then used in the code. One of these constants was missing from the Web.config file and simply adding this setting to the Web.config file resolved the issue.
In my case, this error turned out to be a typo in a read-only local variable. "20017" is not a valid year. Unfortunately, I wasted a lot of time diagnosing. The error message was not helpful.
private static readonly DateTime end_date = Convert.ToDateTime("7/7/20017");
In my case, it was a console app that was instantiating some static variables from config and the config entries were missing:
class Program
{
private static string _databaseConnectionString = ConfigurationManager.ConnectionStrings["database"].ConnectionString;
public static int Main(string[] args)
{
...
}
}
It never hit the breakpoint inside main because it happened as the class was being instantiated, before it hit the main method.
In my case , this error occurred because I added a wrong , useless package. thanks herzmeister for his help.
精彩评论