根据文本框输入查找行

时间:2015-11-18 14:47:33

标签: vb.net

现在我遇到了问题,无法解决问题。 我有本地数据库.SDF并尝试创建一个具有许多用户和密码的登录帐户数据库,对管理员和用户也有限制。

我有一个名为SQLControl的类,这是代码

    Imports System.Data.SqlServerCe

Public Class SQLControl

#Region "Main Declaration"

    Dim SQLConn As SqlCeConnection
    Dim SQLConnString As String = "Data Source=AdmApotikDatabase.sdf"
    Dim SQLCmd As SqlCeCommand

    Dim SQLAdapter As SqlCeDataAdapter
    Dim SQLTable As DataTable

#End Region

    Public Sub LoadData(NewCmdSelect As String)

        Try
            SQLConn = New SqlCeConnection
            SQLCmd = New SqlCeCommand
            SQLConn.ConnectionString = SQLConnString
            SQLCmd.Connection = SQLConn
            SQLCmd.CommandText = NewCmdSelect
            SQLAdapter = New SqlCeDataAdapter(SQLCmd)
            SQLTable = New DataTable

            SQLConn.Open()
            SQLAdapter.Fill(SQLTable)
            SQLConn.Close()


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Public Sub AddData(NewCmdAdd As String)
        Try
            SQLConn = New SqlCeConnection
            SQLCmd = New SqlCeCommand
            SQLConn.ConnectionString = SQLConnString
            SQLCmd.Connection = SQLConn
            SQLCmd.CommandText = NewCmdAdd

            SQLConn.Open()
            SQLCmd.ExecuteNonQuery()
            SQLConn.Close()


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Public Sub EditData(NewCmdEdit As String)
        Try
            SQLConn = New SqlCeConnection
            SQLCmd = New SqlCeCommand
            SQLConn.ConnectionString = SQLConnString
            SQLCmd.Connection = SQLConn
            SQLCmd.CommandText = NewCmdEdit

            SQLConn.Open()
            SQLCmd.ExecuteNonQuery()
            SQLConn.Close()


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Public Sub FreeCmd(NewCmdFree As String)
        Try
            SQLConn = New SqlCeConnection
            SQLCmd = New SqlCeCommand
            SQLConn.ConnectionString = SQLConnString
            SQLCmd.Connection = SQLConn
            SQLCmd.CommandText = NewCmdFree

            SQLConn.Open()
            SQLCmd.ExecuteNonQuery()
            SQLConn.Close()


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

现在我想尝试根据我写的内容找到一行' txtUsername'和&#t;密码'文本框。在这里,我的pbLogin'点击事件:

Public Class LoginForm
    Dim SQLControl As SQLControl
    Private Sub pbLogin_Click(sender As Object, e As EventArgs) Handles pbLogin.Click
        Dim Username As New String
        Dim Password As New String
        Dim IsAdmin As New Integer
        Dim IsUser As New Integer
        If txtUserName.Text <> "" And txtPassword.Text <> "" Then

            Dim AdminCmd As String = "SELECT * FROM TabelAkun WHERE  " & Username & "='admin' AND " & Password & "='admin' AND " & IsAdmin & "= 1"
            SQLControl.FreeCmd(AdminCmd)
            If txtUserName.Text = Username And txtPassword.Text = Password Then
                SQLControl.LoadData("")
                MainWindows.pbAbout.Visible = True
                MainWindows.pbAccount.Visible = True
                MainWindows.pbDataObat.Visible = True
                MainWindows.pbDataSuplier.Visible = True
                MainWindows.pbDataTransaksi.Visible = True
            End If
        Else
            MsgBox("Tidak boleh kosong !")
        End If
    End Sub
End Class

只是结果&#39; NullReferenceException&#39;在&#39; SQLControl.FreeCmd(AdminCmd) 这是我的桌子,叫做TabelAkun&#39;这是它的专栏:

    Dim Username As New String
    Dim Password As New String
    Dim IsAdmin As New Integer
    Dim IsUser As New Integer

我需要有人更正我的点击按钮事件,即“pbLogin&#39;以上根据我的班级Sub,谢谢:)

1 个答案:

答案 0 :(得分:1)

如果您在代码中的该行上设置断点,并查看SQLControl变量的内容,您会看到它设置为Nothing,因为您从未创建过新的实例。

将您的专线Dim SQLControl As SQLControl更改为Dim SQLControl As New SQLControl