简体   繁体   中英

Find a row according from textbox input

now I have a problem, and can't solved my self. I have Local database .SDF and trying to make a Login Account database which have many user and password, also restriction for Admin and users.

I have a class called SQLControl and this is the code

    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

Now I want to try to find a row according to what I write in 'txtUsername' and 'txtPassword' textboxes. Here my 'pbLogin' click event :

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

It just result 'NullReferenceException' at 'SQLControl.FreeCmd(AdminCmd) This is my Table called 'TabelAkun' and this its columns:

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

I need someone to correct my click button event called 'pbLogin' above according to my class Sub, thanks :)

If you set a breakpoint on that line in the code as it is, and view the contents of the SQLControl variable, you'll see that it is set to Nothing because you never created a new instance of it.

Change your line Dim SQLControl As SQLControl to Dim SQLControl As New SQLControl .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM