I am creating an application and I am having difficulty at the first hurdle. I have created a login form consisting of a username and password text box where the the entries must be matched to a record in the database to proceed.
The application is created in Visual Studio Express 2013 and the database is created in Access 2013. When I attempt to compile I am getting the following error message.
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll .
Additional information: Syntax error in FROM clause.
The code for the page is below
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Public Class frm_1_Login
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btn_Login.Click
' Check if username or password is empty
If txt_Username.Text = "" Or txt_Password.Text = "" Then
lbl_LoginWarning.Visible = True
Else
'Connect To Database
Dim conn As New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Kevin\Desktop\Non Conformance\NonConformance.accdb"
Dim sql As String = "SELECT * FROM User WHERE UserName ='" & txt_Username.Text & "' AND UserPassword = '" & txt_Password.Text & "'"
Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql)
'open database connection
sqlCom.Connection = conn
conn.Open()
' Try
Dim sqlRead As System.Data.OleDb.OleDbDataReader = sqlCom.ExecuteReader()
If sqlRead.Read() Then
frm_2_MainMenu.Show()
Me.Hide()
Else
'if user enters wrong username and password combination display error message
lbl_LoginWarning.Visible = True
'clear all fields
txt_Password.Text = ""
txt_Username.Text = ""
'Focus in username field
txt_Username.Focus()
End If
' Catch ex As Exception
'MessageBox.Show("Failed to connect to Database..", "Database Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
'End Try
End If
End Sub
End Class
When its throwing me the error message it is highlighting this line of code afterwards
Dim sqlRead As System.Data.OleDb.OleDbDataReader = sqlCom.ExecuteReader()
I have copied the query into access and that works fine.
I am unsure what is causing this and any help would be much appreciated!
Thanks for reading.
User
is a reserved word
in Access: Reserverd words in access . You must incapsulate reserved words with []
to make em work.
Your query should be
SELECT * FROM [User] WHERE UserName ='" & txt_Username.Text & "' AND UserPassword = '" & txt_Password.Text & "'
And please read up on the use of parameters in your queries, it'll save you much problems later on. Here is some handy info on queries and parameters for OleDb (Access)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.