Solved

VB.net Type Initializer error

Posted on 2013-12-05
12
344 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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
 
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 75

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I need help embedding Base64 Images using VB.Net - When image is a .jpg 2 57
Gridview selected row 9 44
Crystal reports vb.net 2 39
VB.net and sql server 4 33
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 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