我有这个新的vb.net项目(MedicalCost)有这个错误“’MedicalCost.Constants’的类型初始化程序引发了异常.”在运行sub时我已经在我之前的项目中完成了这一切,当我在我的模块上声明公共变量但
这是我模块上的代码(常量):
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
当我在我的frm_add上运行sub时
这是我的代码
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
我注意到从模块声明的任何变量都是这个错误“’MedicalCost.Constants’的类型初始化器引发异常的原因.”发生.
谁能帮我.我花了1个小时搜索这个错误,并没有帮助我.
TNX!
Public ppiconn As New SqlConnection("Dsn=pandiman2002connectdsn;server=ppi;uid=sa;database=Pandimandata2002")
这里的关键是错误消息中的类型初始化程序.当静态数据的初始化引发异常时会发生这种情况.对于VB.Net,它映射到模块的字段或类的共享字段.在这种情况下,错误指向常量,这是唯一的初始化程序,因此它可能会受到责备.
为了找出发生这种情况的原因,您需要找到触发类型初始化程序错误的异常.只需调试应用程序等待错误,扩展InnerException属性,并且应该包含真正的错误