Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

VB.net Type Initializer error

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
Murray Brown
Asked:
Murray Brown
  • 6
  • 5
2 Solutions
 
David L. HansenProgrammer AnalystCommented:
Does the error occur inside the oLoadFile sub or right when the sub it triggered?
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Right when the Sub is triggered. It happens half the time roughly then things work te other half of the time
0
 
David L. HansenProgrammer AnalystCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi. Thanks but that didn't work. All values came in as strings every time and it failed for a few of them
0
 
David L. HansenProgrammer AnalystCommented:
Do you see the line of code in debug mode where the error actually occurs?
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
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
 
David L. HansenProgrammer AnalystCommented:
Can we look at the code in oLoadProjectFile?  Have you stepped into (F11) that Subroutine?
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
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
 
David L. HansenProgrammer AnalystCommented:
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
 
David L. HansenProgrammer AnalystCommented:
Also, get rid of the "Call" word...it is not needed and could be a problem.
0
 
käµfm³d 👽Commented:
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
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much for the advice
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now