0

I'm making a login form and every time i try to login(whether with a correct or not user) it gives me the same error.

Object reference not set to an instance of an object

Here's the code:

Imports System.Data.OleDb
Public Class adminlogin
    Dim ctr As Integer
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Form1.Show()
        Me.Hide()
    End Sub


    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub



    Private Sub Ctl3dPushButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    End Sub



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        conn = New OleDbConnection(cn)
        Try
            conn.Open()
            str = "Select UserName, Password from TblUser where UserName = '" & Trim(txtUserName.Text) & "' and Password = '" & Trim(txtPassword.Text) & "'  and AccessLevel = 'Admin'"
            cmd = New OleDbCommand(str, conn)
            dr = cmd.ExecuteReader
            If dr.HasRows = True Then


                MdiParent.Show()
                Me.Hide()

            ElseIf txtUserName.Text = "" Or txtPassword.Text = "" Then
                MsgBox("Enter Value!")
                txtUserName.Text = ""
                ctr = ctr - 1
                If ctr = 0 Then
                    MessageBox.Show("Hacker's not allowed! End!")
                    End
                End If
            Else
                MsgBox("Invalid UserName/Password")
                txtUserName.Text = ""
                txtPassword.Text = ""
                txtUserName.Focus()
                ctr = ctr - 1
                If ctr = 0 Then
                    MessageBox.Show("Hacker's not allowed! End!")
                    End
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        dr.Close()
        conn.Close()
    End Sub

    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Hide()
        Form1.Show()
    End Sub

    Private Sub Button1_Enter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Button1.KeyPress

        If Asc(e.KeyChar) = Keys.Enter Then
            conn = New OleDbConnection(cn)
            Try
                conn.Open()
                str = "Select UserName, Password from TblUser where UserName = '" & Trim(txtUserName.Text) & "' and Password = '" & Trim(txtPassword.Text) & "'  and AccessLevel = 'Admin'"
                cmd = New OleDbCommand(str, conn)
                dr = cmd.ExecuteReader
                If dr.HasRows = True Then

                    MdiParent.Show()
                    Me.Hide()

                ElseIf txtUserName.Text = "" Or txtPassword.Text = "" Then
                    MsgBox("Enter Value!")
                    txtUserName.Text = ""
                    ctr = ctr - 1
                    If ctr = 0 Then
                        MessageBox.Show("Hacker's not allowed! End!")
                        End
                    End If
                Else
                    MsgBox("Invalid UserName/Password")
                    txtUserName.Text = ""
                    txtPassword.Text = ""
                    txtUserName.Focus()
                    ctr = ctr - 1
                    If ctr = 0 Then
                        MessageBox.Show("Hacker's not allowed! End!")
                        End
                    End If
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            dr.Close()
            conn.Close()
        End If

    End Sub

    Private Sub adminlogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtUserName.Focus()
    End Sub

    Private Sub lblName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblName.Click

    End Sub
End Class
David Wilson
  • 4,264
  • 3
  • 16
  • 30

0 Answers0