Click here to Skip to main content
15,867,686 members
Articles / Database Development / SQL Server

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 35K   29   5
How to make a password verification form in VB that uses LINQ and SQL
Example.JPG

Introduction

This article should be useful for those who need a simple username/password solution for their VB Projects.

Background

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.

VB.NET
Public Class Login
VB.NET
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles OK.Click
        Me.Check_Details()
    End Sub

This Button calls Check_Details Sub to verify user credentials.

VB.NET
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Cancel.Click
        Me.Close()
    End Sub
VB.NET
Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
    Try
        Me.UsersTableAdapter.FillBy(Me.SecurityDataSet1.Users)
    Catch ex As System.Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    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).

VB.NET
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("confirmed")
    Else
        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.

VB.NET
    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 = ""
        Else
            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.

History

  • 21 June 2009 -- First initial article... probably lots of mistakes. I will do better next time.

License

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 
Yeah I know now. I have been reading up on SQL Data types over the last few days. I'm in the middle of changing some datatypes such as image to byte and such.

Will update when I get a chance.

Thanks for the feedback.
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.