FAQ: How do I create a Login Verification routine (Login Form) and make the password case sensitive?
Locked
-
Saturday, April 11, 2009 4:09 PM
How do I create a Login Verification routine (Login Form) and make the password case sensitive?
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
All Replies
-
Saturday, April 11, 2009 4:12 PM
Here is the walkthrough:
1) e.g. MS Access Database file myDB.mdb contains a Users table with the following two fields:
Field Name Data Type
Username Text
Password Text
2) Create a new Windows Forms application, then add a “Login Form” template:
Project menu-> Add Windows Form -> Select "Login Form" template
3) Code sample
Prerequisites: txtUsername TextBox, txtPassword TextBox, OK button and Cancel button on Login Form.
Imports System.Data.OleDb Public Class LoginForm1 ' OK button Private Sub OK_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles OK.Click Dim con As New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=D:\myDB.mdb") Dim cmd As OleDbCommand = New OleDbCommand( _ "SELECT * FROM Users WHERE Username = '" & _ txtUsername.Text & "' AND [Password] = '" & txtPassword.Text & "' ", con) con.Open() Dim sdr As OleDbDataReader = cmd.ExecuteReader() ' If the record can be queried, it means passing verification, then open another form. If (sdr.Read() = True) Then MessageBox.Show("The user is valid!") Dim mainForm As New MainForm mainForm.Show() Me.Hide() Else MessageBox.Show("Invalid username or password!") End If End Sub ' Cancel button Private Sub Cancel_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Cancel.Click Me.Close() End Sub End Class
Related threads:
http://social.msdn.microsoft.com/forums/en/vbgeneral/thread/4eb4dc84-a62f-4a46-95b0-3b981fb83f23/
http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/20a1564c-77ac-4972-9e23-840d63f7622f
Note: The password in the TextBox is not case sensitive in the above code sample.
To make the password case sensitive, try this workaround:
First retrieve the password from database into DataReader object, and then compare it with txtPassword.Text outside T-SQL statement.
Imports System.Data.OleDb Public Class LoginForm1 Private Sub OK_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles OK.Click Dim con As OleDbConnection = New OleDbConnection( _ "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\myDB.mdb;") Dim cmd As OleDbCommand = New OleDbCommand( _ "SELECT * FROM Users WHERE Username='" & _ txtUsername.Text & "' and [Password]='" & txtPassword.Text & "'", con) con.Open() Dim sdr As OleDbDataReader = cmd.ExecuteReader() ' It will be case sensitive if you compare passwords here. If sdr("Password") <> txtPassword.Text.ToString() Then MessageBox.Show("Invalid password!") End If sdr.Close() con.Close() End Sub End Class
Related thread:
http://social.msdn.microsoft.com:80/Forums/en/vbgeneral/thread/cb2e617a-d226-4b20-bff3-11180dfe1b71/
For more FAQ about Visual Basic .NET General, please see Visual Basic .NET General FAQ
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Edited by Xiaoyun Li – MSFT Saturday, April 11, 2009 4:41 PM
- Marked As Answer by Xiaoyun Li – MSFT Saturday, April 11, 2009 4:59 PM
- Edited by Xiaoyun Li – MSFT Tuesday, April 14, 2009 8:03 AM
-
Saturday, April 11, 2009 4:20 PMHi Xiao Yun,
I wonder if you could prepare a walkthrough for Microsoft SQL server? I am using SQL server 2005 express edition :) Thanks a lot!
Thank you, msdn =) 99.9% of my questions have been answered :D -
Sunday, April 12, 2009 11:39 PM
The Login Verification Logic/Routine is still applicable to SQL Server database file.
What you need to do is:
1. Use System.Data.SqlClient instead of System.Data.OleDb namespace
2. Replace such OleDb* classes as OleDbConnection, OleDbCommand, OleDbDataAdapter with corresponding Sql* classes such as SqlConnection, SqlCommand, SqlDataAdapter.
Code sample: How to connect and access SQl Server database and MS Access database in VB.NET
http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/5980181e-f666-4f0a-ab50-c4ebecf96f02/
3. Change database connection string.
Some connection strings for SQL Server 2005
http://www.connectionstrings.com/sql-server-2005
The walkthrough for Microsoft SQL Server database:1) e.g. Prepare a SQL Server database file myDB.mdf containing a Users table with the following two fields:
Field Name Data Type
Username VarChar(20)
Password VarChar(20)
2) Create a new Windows Forms application, then add a “Login Form” template:
Project menu-> Add Windows Form -> Select "Login Form" template
3) Code sample
Prerequisites: txtUsername TextBox, txtPassword TextBox, OK button and Cancel button on Login Form.
Imports System.Data.SqlClient Public Class LoginForm1 ' OK button Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click Dim con As New SqlConnection("Data Source=.;Integrated Security=True;AttachDbFilename=D:\myDB.mdf") Dim cmd As New SqlCommand("SELECT * FROM Users WHERE Username = '" & txtUsername.Text & "' AND [Password] = '" & txtPassword.Text & "' ", con) con.Open() Dim sdr As SqlDataReader = cmd.ExecuteReader() ' If the record can be queried, it means passing verification, then open another form. If (sdr.Read() = True) Then MessageBox.Show("The user is valid!") Dim mainForm As New MainForm mainForm.Show() Me.Hide() Else MessageBox.Show("Invalid username or password!") End If End Sub ' Cancel button Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click Me.Close() End Sub End ClassBest regards,
Martin Xie
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

