简体   繁体   中英

Updating a row in an Access database using OleDb, SQL and VB.NET

I know that there have been many questions on this topic however I cannot find any solutions for my problem. The code that I have created allows the user to create an account (which works perfectly) and right now I am trying to allow the user to change their username and/or password using textboxes. The code reads the details from the database and places them in textboxes to be changed and updated. At the moment, I have managed to get the correct row and locate the correct fields but instead of replacing the current value with the value in the textbox (which I want), -1 or 0 is being placed in to the database in the username column and when I try to change the password, it does not change. The code is below:

 connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\RevisionAid2\RevisionAid2\UserDatabase.accdb"
    Dim conn As OleDbConnection = New OleDbConnection(connstring)

    Dim updateSql As String = String.Format("UPDATE Users SET Username=@USER and [Password]=@PASS WHERE StudentID=@ID")

    Dim cmd As New OleDbCommand(updateSql, conn)
    cmd.Parameters.AddWithValue("@USER", ChangeUsernameTextBox.Text.Trim) 
    cmd.Parameters.AddWithValue("@PASS", ChangePasswordTextBox.Text.Trim)
    cmd.Parameters.AddWithValue("@ID", StudentID.Text)
    'conn.Close()
    conn.Open()
    cmd.ExecuteNonQuery()
    conn.Close()

I have tested the SQL statement in Access and the correct value is returned and the update occurs so I don't know what could be causing this.

Thanks!

Does your updateSql even run?

The query should look like this:

UPDATE Users SET Username=@USER, [Password]=@PASS WHERE StudentID=@ID

And why do you trim username and password?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM