0

I created a login form and when I enter Username and Password then it throws me an error "Object Reference not set to an instance of an object" when I click on Login button here is the code.

Imports System.Data.OleDb
Imports System.Data

Public Class Form1
    Private Sub btnlogin_Click(sender As Object, e As EventArgs) Handles btnlogin.Click
        If Len(Trim(txtusername.Text)) = 0 Then
            MessageBox.Show("Please Enter Username", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtusername.Focus()
            Exit Sub
        End If

        If Len(Trim(txtpassword.Text)) = 0 Then
            MessageBox.Show("Please Enter Password", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtpassword.Focus()
            Exit Sub
        End If

        Try
            Dim myConnection As New OleDbConnection
            myConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "C:\Users\Azam\Desktop\Monitoring DB\MonitoringDB.accdb")
            Dim myCommand As New OleDbCommand
            myCommand = New OleDbCommand("SELECT UserName, Password FROM Users WHERE UserName=@UserName AND Password=@Password")
            Dim uName As New OleDbParameter("@UserName", SqlDbType.VarChar)
            Dim uPassword As New OleDbParameter("@Password", SqlDbType.VarChar)
            uName.Value = txtusername.Text
            uPassword.Value = txtpassword.Text
            myCommand.Parameters.Add(uName)
            myCommand.Parameters.Add(uPassword)
            myCommand.Connection.Open()

            Dim myReader As OleDbDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
            Dim Login As Object = 0
            If myReader.HasRows Then
                myReader.Read()
                Login = myReader(Login)

            End If
            If Login = Nothing Then
                MsgBox("Login failed. Please Try Again", MsgBoxStyle.Critical, "Login Denied")
                txtusername.Clear()
                txtpassword.Clear()
                txtusername.Focus()
            Else
                ProgressBar1.Visible = True
                ProgressBar1.Maximum = 5000
                ProgressBar1.Minimum = 0
                ProgressBar1.Value = 4
                ProgressBar1.Step = 1
                For i = 0 To 5000
                    ProgressBar1.PerformStep()
                Next
                FrmMain.ToolStripStatusLabel2.Text = txtusername.Text
                Me.Hide()
                FrmMain.Show()

            End If
            myCommand.Dispose()
            myConnection.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

End Class
Andronicus
  • 25,419
  • 17
  • 47
  • 88
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – jmcilhinney Feb 23 '19 at 12:37

1 Answers1

0

See if this fixes it.

Private Sub btnlogin_Click(sender As Object, e As EventArgs) Handles btnlogin.Click
    If (Trim(txtusername.Text)).Length = 0 Then 'Don't use Len It is from vb6 and just around for old code
        MessageBox.Show("Please Enter Username", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        'Don't use Focus see docs for why
        txtusername.Select
        Exit Sub
    End If

    If (Trim(txtpassword.Text)).Length = 0 Then
        MessageBox.Show("Please Enter Password", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        txtpassword.Select
        Exit Sub
    End If
    Dim Login As Integer
    Try
        'Whenever you use New you create a new object - don't do it twice
        Using myConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "C:\Users\Azam\Desktop\Monitoring DB\MonitoringDB.accdb")
            'pass both the SQL statement and the to the constructor of the command
            Using myCommand As New OleDbCommand("SELECT Count(*) FROM Users WHERE UserName=@UserName AND Password=@Password", myConnection)
                'You can't use SqlDbType with an OledbCommand
                myCommand.Parameters.Add("@UserName", OleDbType.VarChar).Value = txtusername.Text
                myCommand.Parameters.Add("@Password", OleDbType.VarChar).Value = txtpassword.Text
                myConnection.Open()
                'All we need is the count, not the data
                Login = CInt(myCommand.ExecuteScalar)
            End Using 'The Using blocks ensure that your database objects are closed and disposed
        End Using
        If Login = 0 Then
            MsgBox("Login failed. Please Try Again", MsgBoxStyle.Critical, "Login Denied")
            txtusername.Clear()
            txtpassword.Clear()
            txtusername.Select
        Else
            ProgressBar1.Visible = True
            ProgressBar1.Maximum = 5000
            ProgressBar1.Minimum = 0
            ProgressBar1.Value = 4
            ProgressBar1.Step = 1
            For i = 0 To 5000
                ProgressBar1.PerformStep()
            Next
            FrmMain.ToolStripStatusLabel2.Text = txtusername.Text
            Me.Hide()
            FrmMain.Show()
        End If
        myCommand.Dispose()
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

Of course in a real application you would NEVER store passwords as plain text.

Mary
  • 14,926
  • 3
  • 18
  • 27