开发者

login form using ms access in vb.net

开发者 https://www.devze.com 2022-12-20 22:41 出处:网络
I\'m creating a login form for vb.net using ms access 2003 as database. But it only checks for the username and bypasses the password. Meaning that if the username is correct and the password doesn\'t

I'm creating a login form for vb.net using ms access 2003 as database. But it only checks for the username and bypasses the password. Meaning that if the username is correct and the password doesn't jive with the username, the user can still enter the system. Here is my code:

Try

            Dim NoAcc As String
       开发者_JAVA技巧     Dim NoAccmod2 As String
            Dim NoPas As String

            Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db1.mdb;Jet OLEDB:Database Password=nrew123$%^;")
            Dim cmd As OleDbCommand = New OleDbCommand("Select * from admintable where AdminName= '" & TextBox4.Text & "' ", cn)


            cn.Open()

            rdr = cmd.ExecuteReader
            If rdr.HasRows Then
                rdr.Read()
                NoAcc = rdr("AdminName")
                NoPas = rdr("AdminPass")
                If (TextBox4.Text = NoAcc And TextBox3.Text = NoPas) Then NoAccmod2 = NoAcc

                adminview.Show()



                Me.Hide()
            Else
                MsgBox("Incorrect Username/Password")
                TextBox4.Clear()
                TextBox3.Clear()

            End If
        Catch
            MsgBox("Error logging in, please try again", MsgBoxStyle.Exclamation)
        End Try

How do I do it so that it checks both username and password?


You are using a single line IF .. THEN :
If (TextBox4.Text = NoAcc And TextBox3.Text = NoPas) Then NoAccmod2 = NoAcc so the next line will always be executed:
adminview.Show()

you have to rearrange your IF .. THEN conditions


I will share about login system in vb.net using binding navigator that less of coding. just following the link below! http://www.tesear.com/2011/09/login-system-in-vbnet.html


You could have BOTH the uname and pword in the database and "WHERE" on both, if you get no record back, then you have your answer.


Try using System.String.Compare(String str1,String str2, Boolean ) As Integer like:

If (System.String.Compare(TextBox4.Text, NoAcc, false) And System.String.Compare(TextBox3.Text, NoPas, false)) Then NoAccmod2 = NoAcc


here is the code:

Imports System.Data
Imports System.Data.OleDb
Public Class Form5
    Inherits System.Windows.Forms.Form
    Dim mypath = Application.StartupPath & "\login.mdb"
    Dim mypassword = ""
    Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mypath & ";Jet OLEDB:Database Password=" & mypassword)
    Dim cmd As OleDbCommand

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Hide()
        Dim sql = "SELECT UserID ,PassID FROM MYTAB WHERE USERID='" & TextBox1.Text & "' AND PASSID='" & TextBox2.Text & "'"

        cmd = New OleDbCommand(sql, conn)
        conn.Open()
        Dim dr As OleDbDataReader = cmd.ExecuteReader

        Try
            If dr.Read = False Then
                MessageBox.Show("Authentication failed...")
                Me.Show()
            Else
                MessageBox.Show("Login successfully...")
                Dim frmDialogue As New Form11

                frmDialogue.ShowDialog()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        conn.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Close()
    End Sub


    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Me.Hide()
        Dim frmDialogue As New Form1

        frmDialogue.ShowDialog()
    End Sub


    Private Sub Form5_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Dim frm As New Form1
        frm.Show()
    End Sub
End Class


Try

        Dim NoAcc As String

        Dim NoPas As String
        Dim rdr As OleDbDataReader

        Dim cnn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\mobilestore.accdb;Persist Security Info=False;")
        Dim cmd As OleDbCommand = New OleDbCommand("Select * from logindata where Username= '" & TextBox1.Text & "' and password='" & TextBox2.Text & "'", cnn)

        'NoAcc = TextBox1.Text
        'NoPas = TextBox2.Text



        cnn.Open()
        rdr = cmd.ExecuteReader


        If (rdr.Read()) Then

            NoAcc = rdr("Username")
            NoPas = rdr("password")

            If (TextBox1.Text = NoAcc And TextBox2.Text = NoPas) Then
                Adminpage.Show()
                Me.Hide()
            Else
                MsgBox("Incorrect Username/Password")
                TextBox1.Clear()
                TextBox2.Clear()


            End If





        End If
    Catch
        MsgBox("Error logging in, please try again", MsgBoxStyle.Exclamation)
    End Try
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号