I am getting a run-time error “The type initializer for ‘SensionVB.CommonCall’ threw an exception.” when trying instantiate the object call:
Dim general As New SensionVB.CommonCall
When stepping through the code once it hits the above line in code and fails, it does .
The suggestions from Microsoft are to check for null, or add new neither is true in this case
SensionVB = Project
CommonCall = Public Class with various functions
Public Class CommonCall
Protected WithEvents litOne As System.Web.UI.WebControls.Literal
Public Shared sConnection As String = ConfigurationManager.ConnectionStrings("SerialStamperConnectionString").ConnectionString
Public Shared oConnection As New MySqlClient.MySqlConnection(sConnection)
Public Shared sMaxSerialNumber As String = "S000001"
Public Shared bLoggedin As Boolean = False
Public Shared iPageWidth As Integer = 1024
Public Shared isValidPassword As Boolean = False
Public Shared strCompanyID As String = ""
Public Shared strSecurityID As String = ""
Dim Response As New HttpResponse(HttpWriter.Null)
Const TimeOutInMinutes As Integer = 60
Const SiteTitle As String = "SensiOn"
Public Const PageWidth As Integer = 1250
Public SSOenabled As Boolean
Const SuperUserEmails As String = "super@domain.com"
Public Sub Initialize()
oConnection.Open()
End Sub
I see three lines that may possibly be the culprit:
There are several potential issues here. One, your connection string name (“SerialStamperConnectionString”) is incorrect. If this name does not match a key in the web.config, then it will return
Nothing, causing a NullReferenceException when you attempt to get the value of theConnectionStringproperty.It’s possible that your connection string above is not configured properly in the web.config, thus returning
Nothingas the ConnectionString. If sConnection has been set toNothing, it’s possible that a NullReferenceException could be thrown my the MysqlConnection constructor.If
HttpWriter.NullisNothingthen this could also be the source of your problem (for the same reason noted above).If none of these suggestions seem to be causing the problem, then I would suggest posting more information about your issue (including the stack trace). Offering a million reputation points will not give us any additional information about your code.
ADDENDUM
According to the VB documentation on Shared constructors (and, by extension, members):
You may find it useful to create a Shared constructor in which to initialize your shared fields:
This will not solve your problem, most likely, but it may make it easier to find and to handle (null string, etc.)