Now that you have added all the necessary forms to your project and a menu system, let us now add a login form. This will ensure that only the person that has the account information can login to your system.
But before we add the Login form you will need to add the table first to your database. Use the following SQL code to create a table named “Users”. For more information on how to do this please open “How to Add Table to Your Existing Database”.
USE [LibSys]
GO
/****** Object: Table [dbo].[Users] Script Date: 02/10/2011 14:01:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Users](
[Username] [nvarchar](15) NOT NULL,
[Password] [nvarchar](15) NULL,
[Name] [nvarchar](50) NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
(
[Username] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
Now let us add a “Login” form from the visual studio template.
Adding of Login Form is like adding of Windows Form. To know more on Windows Form please follow the tutorial on “How to Add Windows Forms”.
Double click the OK button and add the following code:
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Dim strPass As String
strPass = GetFieldValue("SELECT Username, Password FROM Users WHERE Username='" & UsernameTextBox.Text & "'", "Password")
If LCase(PasswordTextBox.Text) = LCase(strPass) Then
Dim f As Form = Application.OpenForms.Item("mdiMain")
If f Is Nothing Then
Dim MainForm As New mdiMain
MainForm.Show()
Else
f.BringToFront()
End If
Me.Hide()
Else
MsgBox("Invalid password.Please try again!", MsgBoxStyle.Exclamation)
PasswordTextBox.Focus()
End If
End Sub
In the code above there is a function called “GetFieldValue”. This function will get the value from the “Users” table that you have added recently. You need to add this in your project before you can run the program.
On the project menu click Add module and name it as “modFunction.vb” and paste the following code:
Imports System.Data.SqlClient
Module modFunction
Public Function GetFieldValue(ByVal srcSQL As String, ByVal strField As String)
'create connection
Dim conn As New SqlConnection(My.Settings.LibSysConnectionString)
With conn
If .State = ConnectionState.Open Then .Close()
.Open()
End With
Try
Dim cmd As SqlCommand = New SqlCommand(srcSQL, conn)
'create data reader
Dim rdr As SqlDataReader = cmd.ExecuteReader
'loop through result set
While (rdr.Read)
GetFieldValue = rdr(strField).ToString()
End While
'close data reader
rdr.Close()
Catch ex As SqlException
MsgBox(ex.Message)
Finally
' Close connection
conn.Close()
End Try
End Function
End Module
You may also wish to set the StartPosition property of your Login form to “CenterScreen”.
Now you are ready to test your project if it works. But before that, make sure to add at least one username with password on the users table. You can do this by opening the Management Studio Tools again and adding a record.
Back to Visual Basic .NET 2008 Tutorial.