about updating username/password simple question

parusa

New member
Joined
Feb 9, 2009
Messages
4
Programming Experience
Beginner
i have a form login... that is connected to a ms access database... tables...
the form login will determine if you are already registered to the ms access database...

and it has a option to log in as administrator...

in administrator window you can edit username and password of all the currently registered user to it....

my problem is....

if you will edit username and password it will check if the username that you will change is already registered or not.... if it exist then it will exit if not it will continue to change your username/password...

the problem is the said code for check if exist username is correctly going for the first time you run the form after that without exiting the program

and try to change it again... to other username and password it will not detect if the username is already exist in the record tables

a clear example is this....

i have a field username and password like this in my current tables

test 1234 << the first word is my uername and the 2nd is the password...
test2 1234
test3 1234

i will login test 1234 then i will be transferred to my other form edit username password....

i will choose test 1234 to be changed

i will changed it to test2 1234

the result is username already exist so i will changed it to other username like this

test5 1234 click ok then after that username password successfully changed...

i will open my database record Accounts and i will see the username and password was updated right.... i will not close my edit username password windows... i will close my ms access database

now i will choose test2 1234 to be edit

i will changed it to test5 1234 same as the updated before....

then after clicking ok no username already exists have prompted me...

it says username password successfully changed.... why is it happening...

but when i try to look at the tables of my ms access it has been updated to test5 1234 same as the other...

i want my form to be updated even if i did'nt close it and open it again

please help me with this problem

this is some of my code

Private Sub btnadmin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadmin.Click
Dim logcheck As Boolean
Dim i As Integer
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\Accounts.mdb"
con.Open()
sql = "Select * from tbluserAccount"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "Accounts")
con.Close()
maxrows = ds.Tables("Accounts").Rows.Count
For i = 0 To maxrows - 1


If txtUsername.Text = ds.Tables("Accounts").Rows(i).Item("Username") And txtPassword.Text = ds.Tables("Accounts").Rows(i).Item("Password") And ds.Tables("Accounts").Rows(i).Item("Admin") = True Then

logcheck = True
i = maxrows - 1
Else
logcheck = False
End If
Next i
If logcheck = True Then
Me.Enabled = False
frmAdmin.Show()

Else
MsgBox("Invalid username/password")
End If
ds.Clear()
End Sub

Private Sub btnedit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnedit.Click
Dim index As Integer = ListBoxAccounts.SelectedIndex
Me.Enabled = False
frmEditUserPass.Show()
ds.Clear()
End Sub

Dim ds As New DataSet
Dim con As New OleDb.OleDbConnection
Dim sql As String
Dim maxrows As Integer
Dim index As Integer = frmAdmin.ListBoxAccounts.SelectedIndex
Dim da As New OleDb.OleDbDataAdapter

Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
Dim i As Integer

Dim newUser As String = txtEditUser.Text
Dim newPass As String = txtEditPass.Text
Dim retype As String = txtRetypePass.Text

connectToAccounts()

maxrows = ds.Tables("Accounts").Rows.Count


For i = 0 To maxrows - 1
If retype <> newPass Then
MsgBox("Password do not match")
i = maxrows - 1
ElseIf newPass.Contains(" ") Or retype.Contains(" ") Then
MsgBox("Spacing is not allowed in creating a new password")
i = maxrows - 1
ElseIf newUser.Equals(ds.Tables("Accounts").Rows(i).Item("Username")) Then
MsgBox("Username already exists")
i = maxrows - 1
Else

MsgBox("username/password changed successfully!")

Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("Accounts").Rows(index).Delete()
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("Accounts").NewRow
dsNewRow.Item("Username") = newUser
dsNewRow.Item("Password") = newPass
ds.Tables("Accounts").Rows.Add(dsNewRow)
cb.QuoteSuffix = "["
cb.QuoteSuffix = "]"
cb.QuotePrefix = "]"
cb.QuotePrefix = "["
da.Update(ds, "Accounts")
Dim table As DataTable = ds.Tables("Accounts").GetChanges(DataRowState.Modified)
i = maxrows - 1
Me.Close()
ds.Clear()
connectToAccounts()
End If
Next i

Public Sub connectToAccounts()
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Accounts.mdb"
con.Open()
sql = "SELECT * FROM tbluserAccount"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "Accounts")
con.Close()
End Sub


thanks in advanced for answering
 
problem solution

i already answered my own problem...

i just change my old code


VB.NET:
Expand Collapse Copy
Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
Dim i As Integer

Dim newUser As String = txtEditUser.Text
Dim newPass As String = txtEditPass.Text
Dim retype As String = txtRetypePass.Text

connectToAccounts()

maxrows = ds.Tables("Accounts").Rows.Count


For i = 0 To maxrows - 1
If retype <> newPass Then
MsgBox("Password do not match")
i = maxrows - 1
ElseIf newPass.Contains(" ") Or retype.Contains(" ") Then
MsgBox("Spacing is not allowed in creating a new password")
i = maxrows - 1
ElseIf newUser.Equals(ds.Tables("Accounts").Rows(i).Item( "Username")) Then
MsgBox("Username already exists")
i = maxrows - 1
Else

MsgBox("username/password changed successfully!")

Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("Accounts").Rows(index).Delete()
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("Accounts").NewRow
dsNewRow.Item("Username") = newUser
dsNewRow.Item("Password") = newPass
ds.Tables("Accounts").Rows.Add(dsNewRow)
cb.QuoteSuffix = "["
cb.QuoteSuffix = "]"
cb.QuotePrefix = "]"
cb.QuotePrefix = "["
da.Update(ds, "Accounts")
Dim table As DataTable = ds.Tables("Accounts").GetChanges(DataRowState.Modi fied)
i = maxrows - 1
Me.Close()
ds.Clear()
connectToAccounts()
End If
Next i


to this code

VB.NET:
Expand Collapse Copy
  Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

        Dim maxrows As Integer
        Dim bool As Boolean
        Dim i As Integer
        Dim newUser As String = txtEditUser.Text.ToString
        Dim newPass As String = txtEditPass.Text.ToString
        Dim retype As String = txtRetypePass.Text.ToString
        connectToAccounts()
        maxrows = ds.Tables("Accounts").Rows.Count


        For i = 0 To maxrows - 1
            If retype <> newPass Then
                MsgBox("Password do not match")
                bool = False
                i = maxrows - 1
            ElseIf newPass.Contains(" ") Or retype.Contains(" ") Then
                MsgBox("Spacing is not allowed in creating a new password")
                bool = False
                i = maxrows - 1
            ElseIf newUser = CStr(ds.Tables("Accounts").Rows(i).Item("Username")) Then
                MsgBox("Username already exists")
                bool = False
                i = maxrows - 1
            Else
                bool = True
            End If
        Next i
        If bool = True Then
            MsgBox("username/password changed successfully!")

            Dim cb As New OleDb.OleDbCommandBuilder(da)
            cb.QuoteSuffix = "["
            cb.QuotePrefix = "]"
            cb.QuoteSuffix = "]"
            cb.QuotePrefix = "["

            ds.Tables("Accounts").Rows(index).Item("Username") = newUser
            ds.Tables("Accounts").Rows(index).Item("Password") = newPass
            da.Update(ds, "Accounts")
            ds.AcceptChanges()
            i = maxrows - 1
            ds.Clear()
            connectToAccounts()
            Me.Close()
        End If
    End Sub

admin you can close this topic or leave it open for others to see the solution ive done
 
If you'd appreciate a critique of your code, let me know; I can see quite a few things that I have an opinion on ;)
 
ok go

it's my pleasure to be criticized by professional sir...

go sir it's ok to me

btw i am just a beginner in coding vb.net....

and if you will correct some of my wrong codes its ok sir....

:)
 
Last edited:
VB.NET:
Expand Collapse Copy
Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
Dim i As Integer

Dim newUser As String = txtEditUser.Text
Dim newPass As String = txtEditPass.Text
Dim retype As String = txtRetypePass.Text

[B]'This is a method/sub/function - it should be called [U][SIZE="4"][B]C[/B][/SIZE][/U][/B]onnectToAccounts 
connectToAccounts()

[B]'if this is a strongly typed table, you would use ds.Accounts.Count[/B]
maxrows = ds.Tables("Accounts").Rows.Count

[B]'you can do this like For Each ro as DataRow in ds.Tables("Accounts")[/B]
For i = 0 To maxrows - 1
[B]
'i'm not sure I would do this check inside a loop. surely you would verify the 
'old and new passwords BEFORE you start looping?[/B]
If retype <> newPass Then
[B]'use MessageBox.Show, rather than MsgBox[/B]
MsgBox("Password do not match")
[B]'to exit a for loop, we use the command: Exit For[/B]
i = maxrows - 1

[B]'similarly, I think you should do this check outside of the loop. also it is logical
'that the line If newPass <> retype Then is not true, then newPass and retype
'must be equal in which case there is no need to check them both
'use OrElse rather than Or for checking two conditions unless you specifically
'need the functionality of Or. If youre not sure of the difference, google it[/B]
ElseIf newPass.Contains(" ") Or retype.Contains(" ") Then
[B]'in terms of English language, the message reads better as "Passwords must not contain spaces"[/B]
MsgBox("Spacing is not allowed in creating a new password")
i = maxrows - 1

[B]'if you were using the For Each... pattern above, this line would be much 
'simpler: If newUser.Equals(ro("Username")) Then[/B]
ElseIf newUser.Equals(ds.Tables("Accounts").Rows(i).Item( "Username")) Then
MsgBox("Username already exists")
i = maxrows - 1

[B]'i'm not sure your logic here is correct. if your table has 10 rows then you'll 
'get 9 times "Password changed" and 1 time, get "Username already exists"
'if the username already exists in one row[/B]
Else

[B]'i don't think you should announce success before the operation has been performed[/B]
MsgBox("username/password changed successfully!")

[B]'really don't think this should be done in a loop. for an easier and more readable
'way of doing your data access, see the DW2 link in my sig, section on
'Creating a Simple Data App[/B]
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("Accounts").Rows(index).Delete()
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("Accounts").NewRow
dsNewRow.Item("Username") = newUser
dsNewRow.Item("Password") = newPass
ds.Tables("Accounts").Rows.Add(dsNewRow)

[B]'why do you set these things twice?[/B]
cb.QuoteSuffix = "["
cb.QuoteSuffix = "]"
cb.QuotePrefix = "]"
cb.QuotePrefix = "["

da.Update(ds, "Accounts")

[B]'this is a useless operation. there are no changes after an Update, and you
'dont do anything with the result[/B]
Dim table As DataTable = ds.Tables("Accounts").GetChanges(DataRowState.Modi fied)
i = maxrows - 1
Me.Close()

[B]'why do this?[/B]
ds.Clear()
connectToAccounts()
End If
Next i




VB.NET:
Expand Collapse Copy
  Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

        Dim maxrows As Integer
[B]'do not call variables by useless names like this. booleans should be called something
'that is a logical result like errorsFound or operationSucceeded so you can sa
'If errorsFound Then... If operationSucceded
'be optimistic; name your booleans POSITIVE names, don't call a boolean like
'didntWork because then you have to write If Not didntWork Then.. confusing[/B]

        Dim bool As Boolean
        Dim i As Integer
        Dim newUser As String = txtEditUser.Text.ToString
        Dim newPass As String = txtEditPass.Text.ToString
        Dim retype As String = txtRetypePass.Text.ToString
        connectToAccounts()
        maxrows = ds.Tables("Accounts").Rows.Count


        For i = 0 To maxrows - 1
            If retype <> newPass Then
                MsgBox("Password do not match")
                bool = False
                i = maxrows - 1
            ElseIf newPass.Contains(" ") Or retype.Contains(" ") Then
                MsgBox("Spacing is not allowed in creating a new password")
                bool = False
                i = maxrows - 1
            ElseIf newUser = CStr(ds.Tables("Accounts").Rows(i).Item("Username")) Then
                MsgBox("Username already exists")
                bool = False
                i = maxrows - 1
            Else
                bool = True
            End If
        Next i

[B]'see what I mean? if you'd called this boolean something like userInputIsOkay
'you'd have If userInputIsOkay Then... it reads a lot better!
'your boolean is already a boolean. you don't need to compare it to a boolean
'to get a boolean result to use in your IF. when bool is true, VB will do 
'If True = True Then.. and because True = True is True.. do you see what I mean?
'there is no need to compare booleans to true or false, because they are already
'True or False. no comparison needed[/B]
        If bool = True Then
            MsgBox("username/password changed successfully!")

            Dim cb As New OleDb.OleDbCommandBuilder(da)
            cb.QuoteSuffix = "["
            cb.QuotePrefix = "]"
            cb.QuoteSuffix = "]"
            cb.QuotePrefix = "["

            ds.Tables("Accounts").Rows(index).Item("Username") = newUser
            ds.Tables("Accounts").Rows(index).Item("Password") = newPass
            da.Update(ds, "Accounts")
            ds.AcceptChanges()
            i = maxrows - 1
            ds.Clear()
            connectToAccounts()
            Me.Close()
        End If
    End Sub

admin you can close this topic or leave it open for others to see the solution ive done
There are still quite a few logical problems with it, I wouldnt recommend it as a model solution, sorry.
 
thanks for your correction sir...

i really appreciate it...

i will update my posted code.. and try to do what you've said

:D
 
Back
Top