Click here to Skip to main content
15,119,410 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
I want to know how to redirect on admin form or user form etc on the login form by using by using VB.Net and access database

What I have tried:

VB
Private Sub Btlogin_Click(sender As System.Object, e As System.EventArgs) Handles Btlogin.Click
        Dim warning As String = "Warning!" & vbNewLine & vbNewLine + _
            "If attempt var = 3" + vbNewLine + _
            "System will shut down."
        If (Textuser.Text = "") And (Textpassword.Text = "") Then
            attempt = attempt + 1
            MsgBox("Please input username and password!" & vbNewLine & vbNewLine + _
                   warning, MsgBoxStyle.Exclamation, "Soryy!" & _
                   "Now attempt var = " & attempt)
        ElseIf (Textuser.Text = "") Then
            attempt = attempt + 1
            MsgBox("Please input username!" & vbNewLine & vbNewLine + _
                   warning, MsgBoxStyle.Information, "Soryy!" & _
                   "Now attempt var = " & attempt)
        ElseIf (Textpassword.Text = "") Then
            attempt = attempt + 1
            MsgBox("Please input password!" & vbNewLine & vbNewLine + _
                   warning, MsgBoxStyle.Information, "Soryy ! " & _
                   "Now attempt = " & attempt)
        Else
            Dim strname = Textuser.Text
            Dim strpass = Textpassword.Text
            With objcon
                .Close()
                If .State = ConnectionState.Closed Then
                    .ConnectionString = strconnection
                    .Open()
                    ' MsgBox("connectionState.Open", MsgBoxStyle.Information, "Connected")
                End If
            End With
            ds.Clear()
            strSQL = "Select * From admin Where user_name='" & Textuser.Text & "' And password = '" & Textpassword.Text & "'"
            da = New OleDbDataAdapter(strSQL, objcon)
            da.Fill(ds, "admin")

            If ds.Tables("admin").Rows.Count <> 0 Then
                'play Sound
                My.Computer.Audio.Play(Application.StartupPath + "\Sound\kamal.wav")
                MaximizeBox = True
                MinimizeBox = True
                MsgBox("Log in Successful!", MsgBoxStyle.OkOnly, _
                       "Welcome " + strname)
                Form5.Show()
                Me.Visible = False
            Else
                MaximizeBox = False
                MinimizeBox = False
                attempt = attempt +
                    MsgBox("Ooop!" & strname & " _&_ " & strpass + vbNewLine + vbNewLine + _
                           "The username and password you entered" + vbNewLine + _
                           "Is not valid" + vbNewLine + _
                           "Please try again!", MsgBoxStyle.Exclamation, "Invalid")
            End If
        End If

        If attempt = 3 Then
            MsgBox("Windows is shutting down...", MsgBoxStyle.Critical, "Good bye!")
            Me.Close()
        End If

    End Sub
Posted
Updated 16-Jul-18 3:25am
v2
Comments
CHill60 16-Jul-18 8:25am
   
What is wrong with what you have tried?
Richard Deeming 17-Jul-18 12:18pm
   
SQL Injection; plain-text password storage; selecting all columns from the table just to test how many rows were returned...

I think the better question would be what isn't wrong with the code! :)
CHill60 17-Jul-18 12:28pm
   
Oh I agree. I didn't even bother analysing the code - I was trying to get the OP to explain what their problem was rather than the rather tiring "I don't know how and it's not in Google" scenario

1 solution

Don't know what problem you think you have, but the ones I can see are far worse.

1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you? And on a login page? That's just plain stupid as it doesn't even require me to log in to destroy your DB...


2) Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^] - the code is C#, but it's pretty obvious.

Fix them throughout your app, and then start worrying about moving on - if you don't you will lose your DB.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900