Hello everyone,

I have been coding a Reset Login Details form, the user needs to enter the new Username, the new Password and the Current Password.

Here is my code below - something in the form is not working as i am getting an error when i fill in the form about the INSERT INTO statement.

Imports System.Data.OleDb
Public Class frmChangeLoginDetails 
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Dim con As OleDb.OleDbConnection

        Dim cmd2, mystr As String

        mystr = ("Provider=Microsoft.JET.OLEDB.4.0;" & _
                 "Data Source=|DataDirectory|\PsychologicalStateTracker.mdb")

        con = New OleDb.OleDbConnection(mystr)

        con.Open()
        Dim cmd As OleDbCommand = New OleDbCommand( _
                   "SELECT * FROM tblLogin WHERE Password = '" & txtCurrentPassword.Text & "'", con)

        Dim sdr As OleDbDataReader = cmd.ExecuteReader()
   
        If (sdr.Read() = True) Then

            cmd2 = "INSERT INTO tblLogin (Username,Password) values ('" & txtNewUsername.Text & "','" & txtNewPassword.Text & "')"

            Dim run = New OleDb.OleDbCommand

            Try

                run = New OleDbCommand(cmd2, con)

                run.ExecuteNonQuery()

                MsgBox("The Username and Password have been changed succesfully.")

                con.Close()

            Catch ex As Exception

                MsgBox(ex.Message, MsgBoxStyle.Critical, "Oledb Error")

            End Try
        Else
            MessageBox.Show("Invalid current password!")
        End If
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        frmLogin.Show()
        Me.Hide()
    End Sub
End Class

Recommended Answers

All 9 Replies

Use UPDATE Instead of INSERT INTO..

Like

UPDATE TABLE1 set Username='abc', password='XYZ' where password='PreviousPassword'

What error exactly are you getting?

Have you tried manually performing the insert statement directly into the database?

What are the values of txtNewUsername.Text and txtNewPassword.Text when the insert fails?

Agree with kingsonprisonic. If you want to change details of a record in a table you need to update, not insert. I'm tipping that your error would have been along the lines of cannot insert duplicate entry.

I get an error that there is a syntax error in the insert statement. I have tried the update statement and i still get an error that there is a syntax error in the update statement.

Can you post the error message?

Password is a reserved word in every database.
Use [Password] instead.
With Username im not sure, so better use [Username] as well.

Ok, i have changed the database columns and renamed Username/Password - now the syntax error is gone.

However currently it doesn't seem to be saving the new Username/Password in the database - i am troubleshooting this to see if i can find the issue.

Not sure what the issue is, i am trying to use the UPDATE statement now as follows:

cmd2 = "UPDATE tblLogin set LoginUsername = txtNewUsername.Text, LoginPassword = txtNewPassword.Text where LoginPassword = txtCurrentPassword.Text"

Is this correct?

The error i get is "No value given for one or more required parameters"

Here is the full code of this form (in case needed):

Imports System.Data.OleDb
Public Class frmChangeLoginDetails 
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Dim con As OleDb.OleDbConnection

        Dim cmd2, mystr As String

        mystr = ("Provider=Microsoft.JET.OLEDB.4.0;" & _
                 "Data Source=|DataDirectory|\PsychologicalStateTracker.mdb")

        con = New OleDb.OleDbConnection(mystr)

        con.Open()
        Dim cmd As OleDbCommand = New OleDbCommand( _
                   "SELECT * FROM tblLogin WHERE LoginPassword = '" & txtCurrentPassword.Text & "'", con)

        Dim sdr As OleDbDataReader = cmd.ExecuteReader()
        ' If the record can be queried, it means passing verification.   
        If (sdr.Read() = True) Then

            'cmd2 = "INSERT INTO tblLogin (LoginUsername,LoginPassword) values ('" & txtNewUsername.Text & "','" & txtNewPassword.Text & "')"
            cmd2 = "UPDATE tblLogin set LoginUsername = txtNewUsername.Text, LoginPassword = txtNewPassword.Text where LoginPassword = txtCurrentPassword.Text"

            Dim run = New OleDb.OleDbCommand

            Try

                run = New OleDbCommand(cmd2, con)

                run.ExecuteNonQuery()

                MsgBox("The Username and Password have been changed succesfully.")

                con.Close()

            Catch ex As Exception

                MsgBox(ex.Message, MsgBoxStyle.Critical, "Oledb Error")

            End Try
        Else
            MessageBox.Show("Invalid current password!")
        End If
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        frmLogin.Show()
        Me.Hide()
    End Sub
End Class

OK i have now fixed it and the UPDATE statement works fine:

cmd2 = "UPDATE tblLogin SET LoginUsername = '" & txtNewUsername.Text & "'," _
                & "LoginPassword = '" & txtNewPassword.Text & "'"

Thanks everyone :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.