-1

I am creating a register form for a program however i cannot get the username and password to save into the microsoft access database. I do not see where i am going wrong with my code, i keep getting 'Syntax error in INSERT INTO statement.' Here is my code...

    Dim sql As String = "INSERT INTO Users (username, password) VALUES (" & Me.Usernametxtb.Text & "','" & Me.Passwordtxtb.Text & "')"

        Dim cms As New OleDbCommand(sql, conn)
        cms.Parameters.AddWithValue("@username", Usernametxtb.Text)
        cms.Parameters.AddWithValue("@password", Passwordtxtb.Text)
        cms.ExecuteNonQuery()
2
  • 3
    You are inserting the values directly in your query without an opening single quote. You probably meant to do something like this INSERT INTO Users (username, password) VALUES (@username, @password) (Might be different. You don't show what language you are using or anything.) Commented May 12, 2018 at 9:48
  • i've created a form using vb.net, i'm taking the values of the username and password directly from a textbox displayed on the form which the user fills out Commented May 12, 2018 at 9:59

2 Answers 2

1

You are adding parameters, but your statement is not parameterised. Without referencing those parameters in the SQL statement using @username, they aren't used anywhere. The first line is still vulnerable to SQL injection because you're concatenating raw user input.

Also, do not store passwords in plaintext, ever.

Sign up to request clarification or add additional context in comments.

1 Comment

Apart from what you mention, OP is adding single quotation marks around parameter values in his query. I don't know if this is correct (code samples I've found don't include any) but if it was, still wouldn't work as there is a missing quotation mark before first parameter value.
1

And Password is a reserved word in Access, so try:

Dim sql As String = "INSERT INTO Users (username, [password]) VALUES (@username, @password)"

Dim cms As New OleDbCommand(sql, conn)
cms.Parameters.AddWithValue("@username", Usernametxtb.Text)
cms.Parameters.AddWithValue("@password", Passwordtxtb.Text)
cms.ExecuteNonQuery()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.