?
Solved

VS 2005 VB.NET: Global Module failing to initialize - System.TypeInitializationException was unhandled

Posted on 2007-10-21
18
Medium Priority
?
3,497 Views
Last Modified: 2011-10-03
I have an application that I have been working on for a while now and suddenly something that was working fine just fails - the global module!

The items I have been tweaking lately are not related at all to the global module and I had been running and rerunning the app over and over when one time it just stopped - with that error.  It fails on the first attempt to access a public routine to create the DB connection.

Does anyone know what could have caused it - I mean I honestly made no changes involving the module at all!

I'd show source code, but I can't think of anything relevant to show.
0
Comment
Question by:pfcs_sql_admin
  • 10
  • 6
  • 2
18 Comments
 
LVL 3

Expert Comment

by:davisnw
ID: 20118428
From what you have said, it appears that you have a problem connecting to the database.  Did a database password perhaps change?  Are you experiencing network issues?  A stack trace would help immensely, as there are many things that could cause a problem with a database connection.  Please post the following info:

1. The stack trace of the exception that is generated.
2. Is this a desktop or ASP.NET application?
3. Is the database server on the same server/computer as the VB.NET application?
0
 

Author Comment

by:pfcs_sql_admin
ID: 20118452
The error comes as it tries to call the public routine.  I can't even Step In to the routine because it isn't initializing.  I am having no problems accessing the database (outside the ap), and the error popped up out of the blue on a saturday (i was working overtime - like today) so no one was around to make any password changes, and it worked fine up until then.  The Ap sever and DB server are two different machines.  And this is not an ASP ap.

modGlobal is my module, and CreateDBConn is the subroutine that it cannot even call any more.

The details from the debugger are:

System.TypeInitializationException was unhandled
  Message="The type initializer for 'PF_KeyControl.modGlobal' threw an exception."
  Source="PF_KeyControl"
  TypeName="PF_KeyControl.modGlobal"
  StackTrace:
       at PF_KeyControl.modGlobal.CreateDBConn()
       at PF_KeyControl.frmMain.frmMain_Load(Object sender, EventArgs e) in Q:\Projects_Apps\PF_KeyControl_R\PF_KeyControl\frmMain.vb:line 13
       at System.EventHandler.Invoke(Object sender, EventArgs e)
       at System.Windows.Forms.Form.OnLoad(EventArgs e)
       at System.Windows.Forms.Form.OnCreateControl()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.WmShowWindow(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmShowWindow(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
       at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
       at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
       at System.Windows.Forms.Control.set_Visible(Boolean value)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at PF_KeyControl.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

0
 

Author Comment

by:pfcs_sql_admin
ID: 20118589
Apparently VS has corrupted something.

I tried creating a new module and copy/pasting everything into it, but got the same error.

I have since created a new module and copied only one function at a time out of the old and pasted into the new ... and things seem to be working right now.

I can't even begin to explain it, but it seems to be Visual Studio's fault.

Thanks for taking a look!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 96

Expert Comment

by:Bob Learned
ID: 20139558
Does your modGlobal module have some module-level variables declared?

Bob
0
 

Author Comment

by:pfcs_sql_admin
ID: 20180991
Ok, it did it again, only this time I know an action I took right beforehand, and can replicate it.

On a regular windows form, I was editing the code and had a datagridview event up top in the code view, and then later added a couple more related ones.  As I was needing to edit these more or less together, for convenience I cut the event out from the top and repasted it back in near the other two at the bottom.  It doesn't come up very often, but it IS something I've done before.

The very next time I ran the application, I got the error again.

Mind you, the form I was editing has NOTHING WHATSOEVER to do with the Global Module that is throwing the error.  When I run the application, I instantiate a user object that then calls some of those global functions.  The module is failing to instantiate as mentioned in the posts above.

The form I edited isn't called until the user makes about 3 mouse clicks to navigate to it.  So the edited form itself is NOT the program-logic-cause of the problem.  However, by commenting out the event in the form, I can get the app to run.  So though totally unrelated - the act of cutting and pasting that event IS the cause, though it should have no effect.

Thinking perhaps there is some sort of bizarro caching involved with the building/compiling process, I closed VB, deleted the BIN files of the app, restarted, rebuilt, and re-ran.  Same error.

What in the world is causing this?  This sounds an awful lot like an IDE bug but I can't figure out a (logical) way around it.  I've tried searching M$ site, but their site is incomprehensible when trying to search forums and such.

Has anyone else seen this - and more to the point - does anyone know how to make it stop?  hehe.
0
 
LVL 3

Expert Comment

by:davisnw
ID: 20181919
Can you post the code that you copied and pasted?
0
 

Author Comment

by:pfcs_sql_admin
ID: 20185062
The code I moved is:

    Private Sub dgvSecurity_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvSecurity.CellContentClick

        If ((e.ColumnIndex < 6) Or (e.ColumnIndex > 11)) Then
            Exit Sub
        End If

        'Snag the value that was changed
        Dim dgvRow As DataGridViewRow = dgvSecurity.SelectedRows(0)
        Dim bValue As Boolean = dgvRow.Cells(e.ColumnIndex).Value

        'The application sees what the values *was* not what it is now
        'Since this is a boolean ... Not-It
        bValue = Not bValue


        'Now pass those in
        Call CheckboxChecker(e.ColumnIndex, bValue)

    End Sub

None of the things called in the function refer to the global module either (regardless of the fact that this form isn't loaded when the application starts).  But on Ap start other things DO call the global mod, and it fails to instantiate.  Everything worked fine until I moved this code.
0
 

Author Comment

by:pfcs_sql_admin
ID: 20185275
Wonderful.

As of today, it is worse.  Yesterday, I could comment out the event and the app would work,  Uncomment and it failed.

As of today - even commenting doesn't help.  (No other changes made, as this IS the only issue of importance right now, being terminal as it is.

Oh, to the Learned One above - the global module has ... well all my global variables and routines in it.  So the only module-level variables are declared as public.  Within the various routines there are some local declarations of course.  Not 100% sure I am understanding the question.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20185293
What kind of variables are you defining at module level?  

Is it anything like this?

    Public app As New Word.Application()

Bob
0
 

Author Comment

by:pfcs_sql_admin
ID: 20185429
No external application-type things.

The only global variables defined are to globally-define each form, and an Enum.  Everything else in the module are functions/subs.

0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 20185450
Since VB.NET compiles a module into a class when it executes the code, it is in effect really just a shared class.  That error relates to an error that occurs when the class is instantiated.

Bob
0
 

Author Comment

by:pfcs_sql_admin
ID: 20186028
I just went through and converted my global module into a class.  Well more precisely I created a class, then coped the stuff over, deleted the original module completely, etc.  Then went through and made the changes to call the class.functions throughout the application.

That worked - until I tried to instantiate a form, then the remaining module failed.  I still have the form variables declared as public in a module (and the new class in fact).

The ONLY global items I have in a (fresh) module now are:

    Public gclsGlobal As New clsGlobal
    Public gDBConn As New SqlConnection
    Public gUser As clsUser
    Public gImageList As New ImageList

    'Forms - Finds
    Public gfrmFindLocks As New frmFindLock
    Public gfrmFindKeys As New frmFindKeys
    Public gfrmFindPeople As New frmFindPeople
    Public gfrmFindUsers As New frmFindUsers

    'Forms - Edits
    Public gfrmEditLock As New frmEditLock
    Public gfrmEditKey As New frmEditKey
    Public gfrmEditRing As New frmEditKeyRing
    Public gfrmEditPerson As New frmEditPerson
    Public gfrmEditUAG As New frmEditUAG
    Public gfrmEditFacility As New frmEditFacility
    Public gfrmEditSubArea As New frmEditSubArea
    Public gfrmEditStorage As New frmEditStorage
    Public gfrmEditTemplate As New frmEditTemplate
    Public gfrmEditUser As New frmEditUser

    'Forms - Misc
    Public gfrmSelectPerson As New frmSelectPerson
    Public gfrmSelectKeyRing As New frmSelectKeyRing
    Public gfrmSelectLocation As New frmSelectLocation
    Public gfrmSelectUAG As New frmSelectAccessGroup
    Public gfrmSelectStorage As New frmSelectStorage

    Public Enum AppSecurityValues
        None = 0
        View = 1
        Edit = 2
        Admin = 4
        AppAdmin = 8
        PFCSAdmin = 16
    End Enum

Note that the new class is the first thing - and I had no issues instantiating that variable in order to call class items.  I then got the module failure (on the mod I copied this from) when I tried to open a form - which the variable is declared above.

I have uncovered an unrelated?! or maybe not!? - problem.  I have a DLL that is referenced within the application.  During some of these changeovers it may just ... disappear ... from the Release folder - which throws errors.  It is STILL referenced in the application, and it is still in the Debug folder of the application and as far as I can tell it is running from Debug.  In fact my config file and exe are only in the Debug folder, so it isn';t even possible to run from the Release folder.  However, in removing the old global modules, the DLL also got deleted from the Release folder.  I think this occurred after I chose to Clean the project.

Probably unrelated to the original issue, but you never know when the "Well if you had mentioned THAT ... " might rear itse head hehe.

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20186192
Another thing with 2005 and VB.NET, is that you don't need to have an instance of a form in order to reference it or the properties, events and methods.

Bob
0
 

Author Comment

by:pfcs_sql_admin
ID: 20186287
Hmmm at the risk of fully crossing over the "looking foolish" line ....

How do you keep from getting dirty forms then?  The way I do it is by having a global variable (which you see in the code above) that I set to nothing before setting to a new instance before opening it.  That way I know that it isn't holding anything over from a previous use (those Select or Find forms that you see above for example).

0
 

Author Comment

by:pfcs_sql_admin
ID: 20186379
Ha!  Nevermind that lol.  After about 10 seconds I see how to do that.  /bonks self on head.

:P
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20187094
*ROFL* No problem, grasshopper!!

Bob
0
 

Author Comment

by:pfcs_sql_admin
ID: 20187723
Mr. One,

Yours will probably end up being the solution, but I'm going to give it a bit more time before closing the question - if nothing else so that if I see any more quirks I don't need to create a new thread.

So far I have recoded every single form call in the app to explicitly create a new one, use it, then move on - and in the process removed all those global form references.

As of now, the only global references in my application are:

    Public gclsGlobal As New clsGlobal
    Public gDBConn As New SqlConnection
    Public gUser As clsUser
    Public gImageList As New ImageList

    Public Enum AppSecurityValues
        None = 0
        View = 1
        Edit = 2
        Admin = 4
        AppAdmin = 8
        PFCSAdmin = 16
    End Enum

And - as of now - things seem to be working.  I must say I am EXTREMELY frustrated that I have lost about two days total development time (that I don't have to waste) working through something that is not a coding issue - it's an IDE/Environment issue.

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20188366
1) I tend to stay away from global references, as they are not necessary in almost every case that I can think of (including an SqlConnection)

2) With SQL Server, and connection pooling, a static connection is created, and if the connection strings are the same, there isn't very much advantage of having a single reference to an SqlConnection.

3) Most of the time I use an SqlDataAdapter with command text and connection string, and I let the adapter handle the connections internally.

4) I sympathize with your frustration over the way that the .NET IDE works in relation to other IDEs (you aren't alone).

Bob
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question