0

I am trying to create a forgot password screen for my application. I am using the tab control for the different pages. My current code is able to create a user but it is able to create duplicates (Which is an issue needed to be rectified) I then have an issue with the forget password screen not working completely.

My code is:

Imports System.Data.OleDb

Public Class Form2
Dim connection As New OleDb.OleDbConnection

Dim dbProvider As String
Dim dbSource As String
Dim MyDocumentsFolder As String
Dim TheDatabase As String
Dim FullDatabasePath As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Dim DBTest1 As String
Dim DBTestP1 As String
Dim cmd As New OleDbCommand(sql, connection)
Dim connStr As String
Dim usernamevalid As Integer
Dim passwordvalid As Integer

Public Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
    If User.Text.Length < 4 Then
        usernamevalid = 0
    ElseIf User.Text.Length > 4 Then
        usernamevalid = 1
    End If
    If Pass.Text.Length < 5 Then
        passwordvalid = 0
    ElseIf Pass.Text.Length > 5 Then
        passwordvalid = 1
    End If
    If usernamevalid = 0 Then
        MsgBox("Username Must Be At Least 5 Characters")
    End If
    If passwordvalid = 0 Then
        MsgBox("Password Must Be At Least 5 Characters")
    End If
    If passwordvalid And usernamevalid = 1 And Pass.Text = RePass.Text Then
        dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0;"
        dbSource = "Data Source = C:\Users\Daniel\Documents\Robocopy.accdb"
        Dim connStr = dbProvider & dbSource

        DBTest1 = User.Text
        DBTestP1 = Pass.Text
        sql = "INSERT INTO Robocopy(username,[password],sq,sqa) VALUES('" & DBTest1 & "','" & DBTestP1 & "','" & SQREG.Text & "', '" & SQAREG.Text & "')"

        Using connection = New OleDb.OleDbConnection(connStr)
            Using cmd = New OleDb.OleDbCommand(sql, connection)
                connection.Open()
                cmd.ExecuteNonQuery()
                connection.Close()
                MsgBox("User Created!")
                'With cmd.Parameters
                '.AddWithValue("usernamer", DBTest.Text)
                '.AddWithValue("password", DBTestP.Text)
                '.AddWithValue("email", txtsub.text)
                '.AddWithValue("contactnum", txtau.text)
                'End With
                'cmd.ExecuteNonQuery()
            End Using
        End Using
    ElseIf Not Pass.Text = RePass.Text Then
        MsgBox("Passwords did not match")
    End If
End Sub

Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
    Dim result = MessageBox.Show(" Are you sure you want to quit", "Are you sure?", MessageBoxButtons.YesNoCancel)
    Me.Close()
End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    If NewPass.Text = ReNewPass.Text Then
        Try
            connection.Open()
            cmd = New OleDbCommand("update robocopy set [password] = '" & NewPass.Text & "' where username = '" & UserFGT.Text & "'", connection)
            cmd.ExecuteNonQuery()
            MessageBox.Show("PASSWORD CHANGE SUCCESSFULLY")
            connection.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End If
End Sub
End Class

The exception it catches is The ConnectionString property has not been initialized

Currently I changed my code to what I think you mean:

Button4's code has been changed as such:

   Private Sub ResetPassword_Click(sender As Object, e As EventArgs) Handles ResetPassword.Click
    If NewPass.Text = ReNewPass.Text Then
        Using connection = New OleDb.OleDbConnection(connStr)
            Using cmd = New OleDb.OleDbCommand(sql, connection)
                connection.Open()
                cmd = New OleDbCommand("update robocopy set [password] = '" & NewPass.Text & "' where username = '" & UserFGT.Text & "'", connection)
                cmd.ExecuteNonQuery()
                MessageBox.Show("PASSWORD CHANGE SUCCESSFULLY")
                connection.Close()
            End Using
        End Using

    End If
End Sub

I now get the error for cmd = New OleDbCommand("update robocopy set [password] = '" & NewPass.Text & "' where username = '" & UserFGT.Text & "'", connection) I am getting the error for cmd where it says 'Read Only' Variable cannot be the target of assignment

DaMrZ
  • 11
  • 1
  • 6
  • Have you stepped through the code to see where the error is happening? – Karl Gjertsen Nov 10 '15 at 17:04
  • There are several issues there and I am not sure where in that wall of code I am supposed to look, but `Button4` never (re)creates a valid connection object. Most of those DB objects should be local and do not concat to make SQL use Parameters (which is commented out). Passwords should be hashed, not saved as plaintext – Ňɏssa Pøngjǣrdenlarp Nov 10 '15 at 17:05
  • 1
    Your button names do not tell us which button does what! – Karl Gjertsen Nov 10 '15 at 17:08
  • @KarlGjertsen I will update the button names now sorry – DaMrZ Nov 10 '15 at 17:12
  • you might want to [look at this](http://stackoverflow.com/a/29187199/1070452) - several common errors in your code – Ňɏssa Pøngjǣrdenlarp Nov 10 '15 at 17:13
  • @Plutonix I will look at that now – DaMrZ Nov 10 '15 at 17:21
  • you cant create a new cmd inside a `Using cmd As New OleDBCommand` block. `sql` is not initialized and there are still no params. It is also not clear if `connstr` is initialized. You should study the link I posted. – Ňɏssa Pøngjǣrdenlarp Nov 10 '15 at 17:26
  • @Plutonix I do not understand what you mean by params. (I have basic knowledge of code but not a lot) also I find it difficult to work with databases fullstop. I know this may be a pain but unless you explain it to me in basically plain english I will not understand. Sorry about that. So from what I have where can I go to fix it then improve it? – DaMrZ Nov 10 '15 at 17:30
  • `Parameters` are that block of code in button1 which is commented out (and shown in the link). – Ňɏssa Pøngjǣrdenlarp Nov 10 '15 at 17:38

1 Answers1

0

The only thing I can see wrong with the connection string are extra spaces after 'Data Source='.

Here's an example from connectionstrings.com:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;Persist Security Info=False;
Karl Gjertsen
  • 4,690
  • 8
  • 41
  • 64