Question

I've been trying to implement a login page where a user enters their username and password and the entered username and password are compared with those in a data table. If the username and password match a message appears saying it was successful.

I've been trying to run the code, but I keep getting a syntax error. I get the following message in visual studio:

An unhandled exception of type System.Data.OleDb.OleDbException occurred in System.Data.dll Additional information: Syntax error (missing operator) in query expression 'User ID = 'jtenori1' AND [Password] = '''.

Code is included... Please let me know if you need more info and I appreciate the help in advance!

Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click

    Dim LoginID, Password As String

    LoginID = txtLoginID.Text
    Password = txtPassword.Text

    Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\person\Desktop\experiment\class\Project\Project41\Project41\project41.accdb")
    con.Open()

    Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM UserInfo WHERE User ID = '" & txtLoginID.Text & "' AND [Password] = '" & txtPassword.Text & "' ", con)

    Dim sdr As OleDbDataReader = cmd.ExecuteReader()

    If (sdr.Read() = True) Then

        MessageBox.Show("Successfully Logged In")
        txtLoginID.Enabled = False
        txtPassword.Enabled = False

    Else
        txtLoginID.Text = ""
        txtPassword.Text = ""
        MessageBox.Show("Invalid User ID or Password")
    End If
End Sub
Was it helpful?

Solution

If you have User ID column then you must enclose it in [User ID].

Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM UserInfo 
     WHERE [User ID] = '" & txtLoginID.Text & "' AND [Password] = '" & txtPassword.Text & "' ", con)

OR

Cross check column name used in the query with reference to name in database.

NOTE: It is highly recommended to use parameterized query.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top