Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type initializer for '[Modulename]' threw an exception

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!

like image 887
Juan Filipe Avatar asked Mar 07 '14 05:03

Juan Filipe


2 Answers

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

like image 84
JaredPar Avatar answered Nov 08 '22 09:11

JaredPar


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.

like image 32
user7427506 Avatar answered Nov 08 '22 07:11

user7427506