Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.net Type Initializer error

Posted on 2013-12-05
12
Medium Priority
?
354 Views
Last Modified: 2013-12-05
Hi

I am getting the following error

The type initializer for 'Elements.Read_Write_Text_Files' threw an exception

It happens when I try and all a procedure in that module:

Call  Read_Write_Text_Files.oLoadFile(sPath, sFile)

The procedure begins:

Public Sub oLoadFile(ByVal oFolder As String, ByVal oTextFileName As String)

I can't understand this and when I run other code it suddenly works
0
Comment
Question by:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 39698592
Does the error occur inside the oLoadFile sub or right when the sub it triggered?
0
 

Author Comment

by:Murray Brown
ID: 39698714
Right when the Sub is triggered. It happens half the time roughly then things work te other half of the time
0
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 39698904
In debugger see if you can trap the incoming parameters that are being sent to the Subroutine.  I suspect that the times when it fails you'll see that one of the parameters is not a String.
0
Technology Partners: 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!

 

Author Comment

by:Murray Brown
ID: 39699033
Hi. Thanks but that didn't work. All values came in as strings every time and it failed for a few of them
0
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 39699038
Do you see the line of code in debug mode where the error actually occurs?
0
 

Author Comment

by:Murray Brown
ID: 39699114
The error occurs as it tries to run that procedure. I have added a watch as shown in the image.
Hovering over the variables after the error doesn't show anything
1
0
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 39699260
Can we look at the code in oLoadProjectFile?  Have you stepped into (F11) that Subroutine?
0
 

Author Comment

by:Murray Brown
ID: 39699273
Hi. Here is all the code I  the Subroutine. I have stepped into it when it runs properly but when it doesn't it fails before even getting in there

Public Sub oLoadProjectFile(ByVal oFolder As String, ByVal oTextFileName As String)

        Try

            Dim i As Integer
            Dim r As Integer
            Dim oFileAndPath As String = oFolder & "\" & oTextFileName

            Dim oReader As System.IO.StreamReader
            oReader = System.IO.File.OpenText(oFileAndPath)

            Globals.ThisAddIn.oRIGHT.lblLoadType.Text = "Tables and Columns"

            With oReader

                '+++++++++++++ These 4 needed to run SQL query +++++++++++
                Dim sELEMENTS_TAG As String = .ReadLine 'this is used when the file is double clicked on for file association
                Dim oDataBaseType As String = .ReadLine
                Dim oConnectionString As String = .ReadLine
                Dim oSQL As String = .ReadLine
                'MsgBox(Len(oSQL))
                While Len(oSQL) = 0
                    oSQL = .ReadLine
                End While
                '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                Dim arrSplit As Object = Split(oSQL, "|")
                Dim S As String = ""
                For k As Integer = 0 To UBound(arrSplit)
                    S = S & arrSplit(k)
                Next
                txtSQL.Text = S

                If oDataBaseType = "Access" Then
                    lblDataBaseType.Text = "Access"
                    lblConnectionString.Text = oConnectionString
                    Call Globals.ThisAddIn.oRIGHT.Populate_TreeView_From_Access(oConnectionString)
                    Call Globals.ThisAddIn.oRIGHT.Load_DataGridView1_DropDowns()
                    Call Globals.ThisAddIn.oRIGHT.Load_Access_Where_Examples()
                ElseIf oDataBaseType = "SQL" Then
                    lblDataBaseType.Text = "SQL"
                    lblConnectionString.Text = oConnectionString
                    Call Globals.ThisAddIn.oRIGHT.Populate_TreeView_From_SQL(oConnectionString)
                    Call Globals.ThisAddIn.oRIGHT.Load_DataGridView1_DropDowns()
                    Call Globals.ThisAddIn.oRIGHT.Load_SQL_Where_Examples()
                Else
                    MsgBox("No data to load. Process aborted!")
                    Exit Sub
                End If

                '============ SELECT ====================
                Dim oValue As String

                oValue = .ReadLine


                If oValue = "True" Then
                    Globals.ThisAddIn.oBOTTOM.chkUnique.Checked = True
                Else
                    Globals.ThisAddIn.oBOTTOM.chkUnique.Checked = False
                End If


                For i = 0 To D1.ColumnCount - 1
                    oValue = .ReadLine

                    If oValue <> "" Then
                        D1.Rows(0).Cells(i).Value = oValue
                    End If

                    oValue = .ReadLine
                    If oValue <> "" Then
                        D2.Rows(0).Cells(i).Value = oValue
                    End If

                    oValue = .ReadLine
                    If oValue <> "" Then
                        D3.Rows(0).Cells(i).Value = oValue
                    End If

                    oValue = .ReadLine
                    If oValue <> "" Then
                        If oValue = "True" Then
                            D4.Rows(0).Cells(i).Value = True
                        Else
                            D4.Rows(0).Cells(i).Value = False
                        End If

                    End If

                    oValue = .ReadLine
                    If oValue <> "" Then
                        D5.Rows(0).Cells(i).Value = oValue
                    End If

                    oValue = .ReadLine
                    If oValue <> "" Then
                        D6.Rows(0).Cells(i).Value = oValue
                    End If

                Next i

                oValue = .ReadLine
                txtSelectSQL.Text = oValue
                oValue = .ReadLine
                txtFromSQL.Text = oValue
                oValue = .ReadLine
                txtWhereSQL.Text = oValue
                ' txtSQL.Text = .ReadLine

                '============ FROM ====================
                oValue = .ReadLine
                Dim oDJoins_RowCount As Integer = CInt(oValue) 'Read the row count of the Joins DataGridView
                For q As Integer = 0 To oDJoins_RowCount - 2
                    DJoins.Rows.Add()
                    Form_Joins.DGV_Joins.Rows.Add()
                Next

                For r = 0 To oDJoins_RowCount - 1
                    For i = 0 To 3
                        oValue = .ReadLine
                        If oValue <> "" Then 'Cannot put a blank into a loaded DataGridView ComboBox
                            DJoins.Rows(r).Cells(i).Value = oValue
                            Form_Joins.DGV_Joins.Rows(r).Cells(i).Value = oValue
                        End If
                    Next i
                Next
                Dim oListCount As Integer = CInt(.ReadLine) 'Read the row count of ListBox
                For i = 0 To oListCount - 1
                    ListBoxFromTables.Items.Add(.ReadLine())
                Next


                '============ WHERE ====================
                Where_RTextBox.Text = .ReadLine
                Where_RichTextBox1.Text = .ReadLine
                Dim oUseRadiobutton As String = .ReadLine
                If oUseRadiobutton = "WhereColumnVariables" Then
                    oWhere_Radiobutton_Where_Column_Variables.Checked = True
                Else
                    oWhere_Radiobutton_Where.Checked = True
                End If



                Dim oListCount2 As Integer = CInt(.ReadLine) 'Read the row count of ListBox
                For i = 0 To oListCount2 - 1
                    oWhere_ListBox2.Items.Add(.ReadLine())
                Next

                oWhere_RichTextBox2.Text = .ReadLine
                '=======================================
                Globals.ThisAddIn.oBOTTOM.DataGridView1.AutoResizeColumns()
                .Close()
            End With

            oReader = Nothing


        Catch
            MsgBox("An error occurred in trying to open the report " & oTextFileName)
            MsgBox(Err.Number & Err.Description)
        End Try

    End Sub
0
 
LVL 15

Assisted Solution

by:David L. Hansen
David L. Hansen earned 1000 total points
ID: 39699378
When the error happens, it's not coming through the Catch part of the try then is it (the TRY in oLoadProjectFile)?  Interesting...put a simple MsgBox (or Messagebox.show) just before the "Try" line in the code you just posted and see if that line runs when you do get an error.
0
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 39699390
Also, get rid of the "Call" word...it is not needed and could be a problem.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 total points
ID: 39700213
When you get a "type initializer" error, it means that an exception occurred inside of a constructor. Look through the constructors for your Read_Write_Text_Files class and see if there is any code that might raise an exception.

As a side note:  This is why it's generally not a good idea to have such large methods. It makes tracking down the error much harder. If you break your large methods up into several smaller methods, your stack traces will better reflect where the error occurs.
0
 

Author Closing Comment

by:Murray Brown
ID: 39700241
Thanks very much for the advice
0

Featured Post

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!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

721 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