nikh8610 nikh8610
nikh8610 nikh8610

Reputation: 1

trying to update a passowrd and username in database i get operator error

Imports System.Data.OleDb
Imports System.Data

Public Class Form3
    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim ran As New Random
        TextBox2.Text = ran.Next(1, 8)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Or MaskedTextBox3.Text = "" Then
            MsgBox("Please fill all text boxes With the required info")
        Else
            Dim cmd As OleDbCommand
            Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source =C:\users\nikh8610\Documents\users.accdb")
            Dim str As String
            con.Open()
            str = "UPDATE users SET username = '" & TextBox1.Text & "'WHERE (ID = '" & TextBox2.Text & "') AND password='" & MaskedTextBox3.Text & "' WHERE (ID = '" & TextBox2.Text & "')"
            cmd = New OleDbCommand(str, con)
            cmd.ExecuteNonQuery()
            con.Close()
        End If
    End Sub
End Class

Upvotes: 0

Views: 32

Answers (1)

Sebastian Brosch
Sebastian Brosch

Reputation: 43594

Your query isn't valid. You are using two WHERE parts on the query. Try the following:

str = "UPDATE users SET username = '" & TextBox1.Text & "' WHERE ID = '" & TextBox2.Text & "' AND password='" & MaskedTextBox3.Text & "'"

You also don't UPDATE the password of the user. You can use something like the following to UPDATE the username and password.

str = "UPDATE users SET username = '" & txtUsername.Text & "', password = '" & txtNewPassword.Text & "' WHERE ID = '" & txtUserID.Text & "' AND password = '" & txtOldPassword.Text & "'"

You should also use prepared statements to UPDATE the user information:

Dim cmd As OleDbCommand = New OleDbCommand()
cmd.Connection = con
cmd.CommandText = "UPDATE users SET username = ?, password = ? WHERE ID = ? AND password = ?"

cmd.Parameters.Add("NewUsername", OleDbType.VarWChar, 50)
cmd.Parameters.Add("NewPassword", OleDbType.VarWChar, 50)
cmd.Parameters.Add("UserID", OleDbType.Long)
cmd.Parameters.Add("OldPassword", OleDbType.VarWChar, 50)
cmd.Parameters(0).Value = txtNewUsername.Text
cmd.Parameters(1).Value = txtNewPassword.Text
cmd.Parameters(2).Value = txtUserID.Text
cmd.Parameters(3).Value = txtOldPassword.Text

cmd.Prepare()
cmd.ExecuteNonQuery()

Upvotes: 1

Related Questions