0

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 :)

  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – RianBattle Nov 18 '15 at 15:01

1 Answers1

1

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.

RianBattle
  • 898
  • 6
  • 20
  • I do Dim SQLControl as New SQLControl, now from the exception message I got "There was an error parsing the query. [Token line number = 1, Token line offset = 32, Token in error = =], I think its about my query, any idea? – Bondan's Blackk Diamond Nov 18 '15 at 15:13
  • If your columns are called `Username`, `Password`, and `IsAdmin` then your query should look something like `SELECT * FROM TabelAkun WHERE Username = '" & Username & "' AND Password = '" & Password & "' AND IsAdmin = " & IsAdmin`. All in all, you should really look at parameterizing your query, there are a lot of great articles to help with that both on this site and through google searches. It would likely correct any issue you have with your query. – RianBattle Nov 18 '15 at 15:59