Results 1 to 7 of 7

Thread: Vb.net - Create a login form using a datareader

Threaded View

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,374

    Vb.net - Create a login form using a datareader

    Prerequisits:
    1. 2 Labels
    2. 2 Textbox's
    3. 2 Buttons
    4. MS Access or SQL database


    The 2 TextBox's are named:
    1. txt_Username
    2. txt_Password


    The 2 Button's are named:
    1. btn_OK
    2. btn_Cancel


    The Database's datatable should be named "login" and have 2 datafields. Both fields should be string's. They are named:
    • username
    • password


    Oledb:

    Code:
    Option Strict On
    Option Explicit On
    
    Imports System.Data.OleDb
    Public Class Form1
        Private Sub btn_OK_Click(sender As System.Object, e As System.EventArgs) Handles btn_OK.Click
            'Declare the oledbconnection adn oledbcommand
            Dim con As New OleDbConnection("MyConnectionString")
            Dim cmd As New OleDbCommand("SELECT username,password FROM login WHERE username = ? AND password = ?", con)
    
            'Set up parameters for the oledbcommand
            cmd.Parameters.AddWithValue("username", txt_Username.Text)
            cmd.Parameters.AddWithValue("password", txt_Password.Text)
    
            'If the username or password is empty then throw an error
            If txt_Username.Text = String.Empty Then
                MessageBox.Show("Please enter the username.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
            ElseIf txt_Password.Text = String.Empty Then
                MessageBox.Show("Please enter the password.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                Try
                    'Open the connection and declare an oledbreader
                    con.Open()
                    Dim reader As OleDbDataReader = cmd.ExecuteReader
    
                    'If our reader has one or more rows then read those rows and compare the text
                    If reader.HasRows = True Then
                        reader.Read()
                        'If the username and password match then it's a success
                        If txt_Username.Text = reader.Item("username").ToString And txt_Password.Text = reader.Item("password").ToString Then
                            MessageBox.Show("Login successful")
                        Else
                            'If they don't match than throw an error
                            MessageBox.Show("Login Failed." & Environment.NewLine & _
                                            "Username and password are casesensitive.", _
                                            Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
                        End If
                    End If
                Catch ex As Exception
                    MessageBox.Show(ex.ToString, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
                Finally
                    con.Close()
                End Try
            End If
        End Sub
    
        Private Sub btn_Cancel_Click(sender As System.Object, e As System.EventArgs) Handles btn_Cancel.Click
            Me.Close()
        End Sub
    End Class
    SQL:
    Code:
    Option Strict On
    Option Explicit On
    
    Imports System.Data.SqlClient
    Public Class Form1
        Private Sub btn_OK_Click(sender As System.Object, e As System.EventArgs) Handles btn_OK.Click
            'Declare the sqlconnection adn sqlcommand
            Dim con As New SqlConnection("MyConnectionString")
            Dim cmd As New SqlCommand("SELECT username,password FROM login WHERE username = ? AND password = ?", con)
    
            'Set up parameters for the sqlcommand
            cmd.Parameters.AddWithValue("@username", txt_Username.Text)
            cmd.Parameters.AddWithValue("@password", txt_Password.Text)
    
            'If the username or password is empty then throw an error
            If txt_Username.Text = String.Empty Then
                MessageBox.Show("Please enter the username.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
            ElseIf txt_Password.Text = String.Empty Then
                MessageBox.Show("Please enter the password.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                Try
                    'Open the connection and declare an sqlreader
                    con.Open()
                    Dim reader As SqlDataReader = cmd.ExecuteReader
    
                    'If our reader has one or more rows then read those rows and compare the text
                    If reader.HasRows = True Then
                        reader.Read()
                        'If the username and password match then it's a success
                        If txt_Username.Text = reader.Item("username").ToString And txt_Password.Text = reader.Item("password").ToString Then
                            MessageBox.Show("Login successful")
                        Else
                            'If they don't match than throw an error
                            MessageBox.Show("Login Failed." & Environment.NewLine & _
                                            "Username and password are casesensitive.", _
                                            Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
                        End If
                    End If
                Catch ex As Exception
                    MessageBox.Show(ex.ToString, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
                Finally
                    con.Close()
                End Try
            End If
        End Sub
    
        Private Sub btn_Cancel_Click(sender As System.Object, e As System.EventArgs) Handles btn_Cancel.Click
            Me.Close()
        End Sub
    End Class
    Last edited by dday9; Jun 7th, 2012 at 05:14 PM. Reason: Decided to add the sql equivalent
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width