Solved

VB.net Type Initializer error

Posted on 2013-12-05
12
342 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:murbro
  • 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:murbro
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
 

Author Comment

by:murbro
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:murbro
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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:murbro
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 250 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 74

Accepted Solution

by:
käµfm³d   👽 earned 250 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:murbro
ID: 39700241
Thanks very much for the advice
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now