Login Example in VB.Net

This tutorial we show you how to make a login transaction using Visual Basic.Net and MSSql Server.
How it works?
  1. Design a database that contains username, password and name of the user. 
  2. Make a user interface design. (see figure 1) 
  3. On the button click event make an statement (see code snippet). 
    1. The statement should have sql command that can retrieve records from the database. 
    2. A user validation
Code snippet
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Try
            cm = New SqlClient.SqlCommand
            With cm
                .Connection = cn
                .CommandType = CommandType.Text
                .CommandText = "Select * from Security where username like '" & Me.txtUser.Text & "' and password like '" & Me.txtPass.Text & "'"
                dr = .ExecuteReader
            End With
            If dr.HasRows = True Then
                MsgBox("Welcome!" & dr.items(2).ToString(), MsgBoxStyle.Information)
                Me.Hide()
                frmMain.Show()
                frmMain.StatusStrip1.Items(0).Text = "User"
            Else
                MsgBox("Access Denied!", MsgBoxStyle.Critical)
            End If
            dr.Close()
            Exit Sub
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
            dr.Close()
        End Try
    End Sub

Screenshot
Figure 1
Login Form

Programming Language: VB.Net
Programming IDE: Visual Studio 2010
Database: MS Access