Click here to Skip to main content
15,606,657 members
Articles / Database Development / SQL Server
Posted 22 Jun 2009


29 bookmarked

VB LINQ SQL Password Verify For Beginners

Rate me:
Please Sign up or sign in to vote.
4.86/5 (4 votes)
22 Jun 2009CPOL2 min read
How to make a password verification form in VB that uses LINQ and SQL


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 "UserName", "PssWrd" and "UserActive". You can call all of these columns anything you want.
The username and 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 true or 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) _
Handles OK.Click
    End Sub

This Button calls Check_Details Sub to verify user credentials.

Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Cancel.Click
    End Sub
Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
    Catch ex As System.Exception
    End Try
End Sub

On Form load, this code tells the form to load database details and fill the appropriate boxes (On this form, it fills the UserNameComboList Box).

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", _
                    "Password Problem")
    End If
End Sub

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.
The 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 command.
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) _
Handles unmaskpw.CheckedChanged
        If unmaskpw.Checked = True Then
            Me.PassWordBox.PasswordChar = ""
            Me.PassWordBox.PasswordChar = "$"
        End If
    End Sub
End Class

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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

Questionnvarchar(5)? Pin
Spirch25-Jun-09 11:24
Spirch25-Jun-09 11:24 
AnswerRe: nvarchar(5)? Pin
SteveHarrisBeast28-Jun-09 20:42
SteveHarrisBeast28-Jun-09 20:42 
AnswerRe: nvarchar(5)? Pin
jkhuang6-Nov-10 2:32
jkhuang6-Nov-10 2:32 
GeneralMy 2 cents Pin
Andre Sanches (alvs)22-Jun-09 16:21
Andre Sanches (alvs)22-Jun-09 16:21 
GeneralRe: My 2 cents Pin
SteveHarrisBeast28-Jun-09 20:53
SteveHarrisBeast28-Jun-09 20:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.