How to Add Login Form

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.

Add Login Form

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”.

Login Form

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.

Comments

Submitted byAnonymous (not verified)on Wed, 05/04/2011 - 08:38

When I Run this Program, it appear: An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) I don't know this reason. Thank you. And I need all login Screen with VB.net 2008 Code. thank you again
Submitted byAnonymous (not verified)on Fri, 05/27/2011 - 15:30

this software is good
Submitted byAnonymous (not verified)on Thu, 10/20/2011 - 09:57

This teaching is very helpful for my mini project.. God bless u. hv a nic day
Submitted byudam (not verified)on Tue, 10/22/2013 - 09:25

i want to study a program that relate to the program is bte
Submitted bykhuboo (not verified)on Thu, 11/07/2013 - 18:03

please tell me how to make login page in vb.net2008 which is connecte with database sql server and by this ligin page three can looged in through the same page for defferent environments

Add new comment