This article should be useful for those who need a simple username/password solution for their VB Projects.
I'm only a noob to VB programming and this is my first CodeProject article. I searched the internet for ages, just looking for some good example code, but could not find any. So I had to start from scratch and in the end result I ended up using VB, LINQ and SQL. I use Visual Studio 2008 and Microsoft SQL Server 2008 Express Edition with full text searching installed on top of Windows XP Professional SP3. Don't know if this helps but just thought I would tell you what platform I am using.
Using the Code
All the code below is the background code for the login form. The form calls upon a SQL database via LINQ.
The main table I call upon is called "
users" and the main columns that I query upon are the "
PssWrd" and "
UserActive". You can call all of these columns anything you want.
password columns are
nvchar(25)s and the
useractive column is
nvchar(5). The reason for
nvchar(5) on the
useractive column is because I like to use boolean logic and I couldn't figure out how to make a yes or no column in SQL, so I just throw in a
false into the data tables for my users.
Public Class Login
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
This Button calls
Check_Details Sub to verify user credentials.
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Catch ex As System.Exception
On Form load, this code tells the form to load database details and fill the appropriate boxes (On this form, it fills the
Private Sub Check_Details()
Dim UName As String = Me.UserNameBox.Text.ToString.Trim
Dim PWord As String = Me.PassWordBox.Text.ToString.Trim
Dim sd As New SecurityDataSetTableAdapters.UsersTableAdapter
Dim query = From check In sd.GetData _
Select check.LogonName, check.PssWrd, check.UserActive _
Where LogonName = UName AndAlso PssWrd = _
PWord AndAlso UserActive = "True"
If query.Count() = 1 Then
MessageBox.Show("Password Is Incorrect or no longer Valid", _
This command checks to see if the inputted user credentials are correct. The declarations convert the text in the form's boxes into
string values for use with LINQ SQL Queries. The second set of declarations convert the tableadapter sequence into a short
string name for use with our SQL Query. The Query checks whether or not the inputted username and password match. It also validates if the current user is Active or Inactive.
IF command executes our SQL Query and tells it to count how many replies there are. If the count does not produce a response of 1 (Which is what it should come up with), it will fall over to the second part of the
If the result is positive, the
IF command will run any command under it.
Private Sub unmaskpw_CheckedChanged_
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
If unmaskpw.Checked = True Then
Me.PassWordBox.PasswordChar = ""
Me.PassWordBox.PasswordChar = "$"
Here I have a Check box on my form that turns on/off password masking.
I hope you found this information and code helpful. Later I will probably work on this a bit more by adding a security management section and hashing/encryption for the password.
Points of Interest
I found that I can manipulate VB code easier than PHP. I might fool around with it a bit more.
- 21 June 2009 -- First initial article... probably lots of mistakes. I will do better next time.