Home > Mobile >  How can I solve System.Data.OleDb.OleDbException: 'Syntax error in FROM clause
How can I solve System.Data.OleDb.OleDbException: 'Syntax error in FROM clause

Time:08-25

Imports System.Data.OleDb
Imports System.Data

Public Class Form1
    Dim connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Dimph\OneDrive\Desktop\Richfield BSC IT --- 1st year\Second Semester\Programming 512\Assignment\WinFormsApp1\WinFormsApp1\bin\Debug\net6.0-windows\Database2.accdb")
    '
    Private Sub btnLogin(sender As Object, e As EventArgs) Handles Button1.Click
        If txtUsername.Text = Nothing Or txtPassword.Text = Nothing Then
            MsgBox("Enter Credentials", MsgBoxStyle.Exclamation)
        Else
            If connection.State = ConnectionState.Closed Then
                connection.Open()
            End If

            Dim cmd As New OleDbCommand("select count(*) from User where UserName=? and Password=?", connection)
            cmd.Parameters.AddWithValue("@1", OleDbType.VarChar).Value = txtUsername.Text
            cmd.Parameters.AddWithValue("@2", OleDbType.VarChar).Value = txtPassword.Text
            Dim count = Convert.ToInt32(cmd.ExecuteScalar())

            If (count > 0) Then
                SplashSCreen.Show()
            Else
                MsgBox("Account not found on Database")
            End If
        End If
    End Sub

    Private Sub btnReset(sender As Object, e As EventArgs) Handles Button2.Click

    End Sub

    Private Sub btnSignUp(sender As Object, e As EventArgs) Handles Button3.Click
        Form2.Show()
        Me.Hide()
    End Sub

    Private Sub btnClose(sender As Object, e As EventArgs) Handles Button4.Click
        Me.Close()

    End Sub
End Class

CodePudding user response:

User and Password are both reserved words and using them as object names can confuse the db engine. If you can't rename them, you can bracket them in your SQL so the db engine understands you're referring to objects.

select count(*) from [User] where UserName=? and [Password]=?

Or you could do it this way with fewer brackets:

select count(*) from [User] AS u where u.UserName=? and u.Password=?
  • Related