Prerequisits:
- 2 Labels
- 2 Textbox's
- 2 Buttons
- MS Access or SQL database
The 2 TextBox's are named:
- txt_Username
- txt_Password
The 2 Button's are named:
- btn_OK
- btn_Cancel
The Database's datatable should be named "login" and have 2 datafields. Both fields should be string's. They are named:
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