类型初始值设定项<module>错误

时间:2018-02-02 06:24:17

标签: sql-server vb.net datagridview

我正在搜索并在datagridview上检索它,但是我收到了这个错误&#34;类型初始化程序引发异常。&#34;,在我点击仪表板中的employeesView按钮后(菜单) )。怎么解决?谢谢你们!

错误:

    Private Sub btnEmployees_Click(sender As Object, e As EventArgs) Handles btnEmployees.Click
        employeesView.TopLevel = False
        Me.panelMain.Controls.Add(employeesView)
        Me.panelHeader.BackColor = Color.FromArgb(0, 160, 220)
        employeesView.BringToFront()
        employeesView.Show()
    End Sub

publicVariables.vb

Imports System.Data.SqlClient

Module publicVariables

    Public connection As SqlConnection
    Public command As SqlCommand
    Public dataReader As SqlDataReader
    Public dataAdapter As SqlDataAdapter

    Public dataTable As DataTable

    Public search_query As String
    Public insert_query As String
    Public delete_query As String
    Public update_query As String

    Public result As Integer

    Public getNumber As String = "0"

    Public productsView As New Products
    Public dashboardView As New Dashboard
    Public employeesView As New Employee
    Public salesView As New Sales
    Public logsView As New LogManager

End Module

搜索代码:

Public Sub retrieveEmployeesforSearch()
        Try
            dbConnection()
            search_query = "SELECT * FROM tblemployee_information as emp_info INNER JOIN tblemployee_account as emp_account ON emp_account.employee_number = emp_info.employee_number WHERE emp_info.employee_first_name LIKE @employee_first_name ORDER BY emp_info.employee_first_name;"
            command = New SqlCommand
            With command
                .Connection = connection
                .CommandText = search_query
                .Parameters.Clear()
                .Parameters.AddWithValue("@employee_first_name", employeesView.txtSearchEmployee.Text)
                .ExecuteNonQuery()
            End With
            dataTable = New DataTable
            dataAdapter = New SqlDataAdapter
            dataAdapter.SelectCommand = command
            dataAdapter.Fill(dataTable)
            employeesView.EmployeeDGV.DataSource = dataTable
        Catch ex As SqlException
            MsgBox("Error : " + ex.Message)
        Finally
            connection.Close()
            command.Dispose()
        End Try
    End Sub

如果textbox.text textchanged

,则检索搜索数据
Private Sub txtSearchEmployee_TextChanged(sender As Object, e As EventArgs) Handles txtSearchEmployee.TextChanged
        retrieveEmployeesforSearch()
    End Sub

1 个答案:

答案 0 :(得分:0)

我不知道你的意思,但我认为这段代码也有问题:

search_query = "SELECT * FROM tblemployee_information as emp_info INNER JOIN tblemployee_account as emp_account ON emp_account.employee_number = emp_info.employee_number WHERE emp_info.employee_first_name LIKE @employee_first_name ORDER BY emp_info.employee_first_name;"

您可以参考this link。希望它能帮到你。