Click here to Skip to main content
14,743,331 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I designed as for in visual basic.net. This form a user must enter login detail i.e. username and password to open main form. users are created in access database. The challenge I received error message on login button in the form. and the message says "End
of statement expected" I attached the code

What I have tried:

Quote:
Try
'conn.Open()
'MsgBox("Susscess")

Dim sql As String = "SELECT * FROM tbl_user WHERE username='" & txtUsername.Text & "' AND password = '" & txtPassword.Text & "'"
Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql)

'Open Database Connection
sqlCom.Connection = conn
conn.Open()
Posted
Updated 18-Jun-20 4:37am

1 solution

To resolve your issue, please read this: End of statement expected - Visual Basic | Microsoft Docs[^]
If it's a hole code, then you need to know that Try instruction should ends with End Try. See: Try...Catch...Finally statement - Visual Basic | Microsoft Docs[^]

There's few other things to improve...
1. Your code is sql injection vulnerable. See: SQL Injection - SQL Server | Microsoft Docs[^]
You have to use parameters!

2. You shouldn't store password as a plain-string. Please, read this: Password Storage: How to do it.[^]

3. You have to use Using Statement - Visual Basic | Microsoft Docs[^] to manage disposable resources (objects).

So...
Dim ConnString As String = "..."
Dim dt As DataTable = New DataTable()
Dim sCommand As String = "SELECT * FROM Table1 WHERE SomeField=?;"
Using conn As New OleDbConnection(connString)
    conn.Open()
    Using cmd As New OleDbCommand(sCommand, conn)
        cmd.Parameters.AddWithValue("?", SomeValue)
        Using reader AS OleDbDataReader = cmd.ExecuteReader()
            dt.Load(reader)
        End Using
    End Using
End Using
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900