Introduction
This is a control which will eases the work of creating the login form for Authentication.
Background
We see it is a tiresome work for creating the authentication form for the projects. This control has all the built in feature for validating the user. With this, we can use select query or Stored Procedures to validate a user from the Database (SQL Server 2000).
Setting it Up
First, after adding the control to the form, if you want to use stored procedures for validating the user, you can just select the following properties:
Selection Mode = StoredProcedure
ConnectionString = server=ServerName\InstanceName;database=Databasename;user id=sa;
password=pass;min pool size=1; max pool size=100
StoredProcedureName = SP_ValidateLogin
UserIdParameter =@UserId
UserPasswordParameter = @UserPassword
or else if you want to use SelectQuery
for validating the user, you can just select the following properties:
Selection Mode = SelectQuery
ConnectionString = _
server=ServerName\InstanceName;database=Databasename;user id=sa;
password=pass;min pool size=1; max pool size=100
TableName = TBLUserTable
UserIdColumnName = vUser_LoginName
UserPasswordColumnName = vUser_Password
I have created two events for validating. One will get fired when the user exists and the other if the user does not exists.
The code is:
Private Sub UcLogin_OnUserExists(ByVal drUserInfo As System.Data.DataRow) _
Handles UcLogin.OnUserExists
Try
MessageBox.Show("Valid User", "Barathan Login Control", _
MessageBoxButtons.OK, _
MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show("Error while Validating User", _
"Barathan Login Control", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub UcLogin_OnUserInvalid() Handles UcLogin.OnUserInvalid
Try
MessageBox.Show("Invalid User", "Barathan Login Control", _
MessageBoxButtons.OK, _
MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show("Error while Validating User", "Barathan Login Control", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
That's it! You are done with authentication. This control is very easy to use.
The code
To raise events, I have declared two public events:
Public Event OnUserExists(ByVal drUserInfo As DataRow)
Public Event OnUserInvalid()
In this event, I have specified the delegate as
Datarow
for
OnUserExists
event. This may be useful for using the retrieved data.
Dim user_type as string = drUserInfo("UserType")
The actual code which raises these events are as follows:
Private Function CheckLogin() As Boolean
Try
If Me.SelectionMode = QueryType.SelectQuery Then
If Me.ExecuteSelectQuery = False Then
RaiseEvent OnUserInvalid()
End If
Else
If Me.ExecuteStoredProcedure = False Then
RaiseEvent OnUserInvalid()
End If
End If
Catch ex As Exception
End Try
End Function
The code for validating the user login if the selection mode is set to StoredProcedure
:
Private Function ExecuteStoredProcedure() As Boolean
Try
Dim dsUser As New DataSet
Dim Conn As New SqlConnection(Me.ConnectionString)
Conn.Open()
Dim myCmd As New SqlCommand(Me.StoredProcedureName, Conn)
Dim PrmUserId As SqlParameter = myCmd.Parameters.Add(Me.UserIdParameter, _
Me.TxtUsername.Text.ToString().Trim())
Dim PrmPassword As SqlParameter = _
myCmd.Parameters.Add(Me.UserPasswordParameter, _
Me.TxtPassword.Text.ToString.Trim())
myCmd.CommandType = CommandType.StoredProcedure
Dim MyDa As New SqlDataAdapter(myCmd)
MyDa.Fill(dsUser)
Conn.Close()
If dsUser.Tables(0).Rows.Count > 0 Then
RaiseEvent OnUserExists(dsUser.Tables(0).Rows(0))
Return True
Else
Return False
End If
Catch ex As Exception
MessageBox.Show("Error While Executing the Stored Procedures", _
"Executing Stored Procedure Error", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return False
End Try
End Function
So finally, when clicking the Login button, the following code will check for valid properties and textboxes:
Private Sub btnlogin_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnlogin.Click
Try
If Me.CheckProperties = True Then
If Me.CheckTextBoxes = True Then
Me.CheckLogin()
End If
Else
Me.Dispose()
Application.Exit()
End If
Catch ex As Exception
Throw ex
End Try
End Sub
Stored Procedure
The Stored Procedure that I use to validate the login is:
Create procedure dbo.LOGINSP_ValidateLogin
@Userid varchar(50),
@UserPassword varchar(50)
as
BEGIN
select iUser_id as userid,
vUser_LoginName LoginName,
bUser_Main as Type
from TBLUSERTABLE
where
vUser_LoginName =@Userid
and vUser_Password=@UserPassword
END
Next time, we will see the control with validating the user type also.