I have this new vb.net project (MedicalCost) having this error "The type initializer for 'MedicalCost.Constants' threw an exception." when running the sub.. I've already done this before on my previous project and everything works fine when I'm declaring ang public varible on my module but now in my current project its not working.
here is my code on my module(Constants):
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Data.Odbc
Imports System.Windows.Forms
Public Module Constants
Public ppiconn As New SqlConnection("Dsn=pandiman2002connectdsn;server=ppi;uid=sa;database=Pandimandata2002")
'Dsn=pandiman2002connectdsn;description=PPI Database;uid=sa;app=Microsoft® Visual Studio® 2010;wsid=CRWUSER17-PC;database=Pandimandata2002
Public da As New SqlDataAdapter
Public comm As New SqlCommand
Public dr As SqlDataReader
Public ds As New DataSet
Public x As String
End Module
when im running the sub on my frm_add
here is my code
Sub search_crew()
Try
x = "(isnull(ltrim(rtrim(firstname)),'') + ' ' + isnull(ltrim(rtrim(mi)),'') + ' ' + " _
& "isnull(ltrim(rtrim(lastname)),'') like '%" & Replace(searchbox.Text, " ", "%") & "%' " _
& " or isnull(ltrim(rtrim(lastname)),'') + ' ' + isnull(ltrim(rtrim(mi)),'') + ' ' + " _
& "isnull(ltrim(rtrim(firstname)),'') like '%" & Replace(searchbox.Text, " ", "%") & "%' " _
& "or legal_records.caseno like '%" & UCase(searchbox.Text) & "%')"
ppiconn.Close()
ppiconn.Open()
Pandimandata2002DataSet.EnforceConstraints = False
da = New SqlDataAdapter(select_tblcrw & "where " & x, ppiconn)
da.Fill(Pandimandata2002DataSet.tblCrew)
da.Dispose()
ppiconn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
I noticed that any variable declared from module is the reason why this error "The type initializer for 'MedicalCost.Constants' threw an exception." occurs.
can anyone help me. I already spent 1 hour searching for this error and nothings helps me. tnx!
The problem is almost certainly the following line of code
Public ppiconn As New SqlConnection("Dsn=pandiman2002connectdsn;server=ppi;uid=sa;database=Pandimandata2002")
The key here is type initializer
in the error message. That happens when the initialization of static data throws an exception. For VB.Net this maps to the fields of Modules
or Shared
fields of Class
. In this case the error points to Constants
and this is the only initializer hence it is likely to blame.
In order to find out why this happens you will need to find the exception which triggered the type initializer
error. Simply debug into the application wait for the error, expand the InnerException
property and that should contain the real error
Had this problem with Initializer in a module for Public definitions -- Moved the initializer To the Form using it -- Still as Public. The Problem disappeared.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With