Hey Everyone
I've written a bit of code that is meant to compare a username textbox and password textbox against an entry in a database. However when I type the username and password and click the ok button by application returns with the error message. "OleDbException was unhandled, Syntax error in FROM Clause". This comes from the line:
Here's all of my code:
In my database I have multiple columns in which one of them is called Username and the other UserPassword.
Thank you for reading :)
Coro
I've written a bit of code that is meant to compare a username textbox and password textbox against an entry in a database. However when I type the username and password and click the ok button by application returns with the error message. "OleDbException was unhandled, Syntax error in FROM Clause". This comes from the line:
Code:
Dim password As String = UserLoginCommand.ExecuteScalar()
Code:
Imports System.Data.OleDb
Public Class Login
Dim LoginAttempt As Integer = 0
'Number of attempts at login declaration
Dim AllowedAttempts As Integer = 3
'Number of the maximum attempts to login declaration
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If String.IsNullOrWhiteSpace(txtUsername.Text) Or String.IsNullOrWhiteSpace(txtPassword.Text) Then
MsgBox("Please enter a Username and Password.")
Else
If LoginAttempt = AllowedAttempts Then
MsgBox("Too many failed attempts, please contact your administrator.")
Else
Dim CN As New OleDbConnection(ConnectionString) 'Declares the connection using the database connection modul
CN.Open()
Dim Query As String = "SELECT [UserPassword] FROM User WHERE Username=" & txtUsername.Text & "'"
Dim UserLoginCommand As New OleDbCommand(Query, CN)
Dim password As String = UserLoginCommand.ExecuteScalar()
If txtPassword.Text = password And password <> "" Then
LoginAttempt = 0
MsgBox("authorized access")
Else
LoginAttempt = LoginAttempt + 1
MsgBox("Unauthorized access. Please re-try or contact your administrator." & vbCrLf & ("You have ") & AllowedAttempts - LoginAttempt & " attempt/s remaining.")
End If
CN.Close()
'Closes the connection
CN.Dispose()
End If
End If
End Sub
End Class
Thank you for reading :)
Coro