Reputation: 7
this may be a silly error within my code but when trying to create a new username and password within my program, it says "error" rather than "success". the database is linked fine as i can access it and login using the credentials but cannot create new users.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If txtUser.Text = "" Or txtPass.Text = "" Then
txtUser.Text = "fill in the details"
Else
Try
Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Login.accdb")
Dim insert As String = "Insert into users (Username, Password) values('" & txtUser.Text & "','" & txtPass.Text & "');"
Dim cmd As New OleDbCommand(insert, conn)
conn.Open()
cmd.ExecuteNonQuery()
MsgBox("success")
Catch ex As Exception
MsgBox("error")
End Try
End If
End Sub
the only issue I believe it could be is that I also have an ID section within the database that may not be incremented by 1 but I'm not sure how to incorporate that into the code and I don't think it should be an issue any help or errors noticed would be greatly appreciated
Upvotes: 0
Views: 35
Reputation: 54487
The specific issue here - at least, one specific issue that I can see - is that Password
is a reserved word in Jet/ACE SQL, so it needs to be escaped if used as an identifier. In a real-world application, you should be hashing your passwords and the column might then be named PasswordHash
, so there's no issue. In this case though:
INSERT INTO Users (Username, [Password]) ...
Upvotes: 1