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