Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Articles » .NET Framework »
Generic Login Control
|
Download demo project - 103 Kb
Introduction This is a control which will ease the work of creating the login form for Authentication.
Background We see it is the tiresome work for creating the authentication form for the projects. So this control has all the built in feature for validating the user. With this we can use select query or Stored Procedures to validate 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 he 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 (Please change to your servername/instancename, Userid and passoword ) StoredProcedureName = LOGINSP_ValidateLogin UserIdParameter =@UserId UserPasswordParameter = @UserPassword i have created two event for validating the one will get fired when the user exists and the other if the user does not exists. the code is
' Event is fired when the user exists and it will have the validated datarow so you can use this datarow for using in the application ' eg: User id, User Type ,.. etc.
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
' Event is fired when the user is invalid
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
thats it you are done with Authentication. This control is very easy to use. The code To raise the event i have declared two Public Events
Public Event OnUserExists(ByVal drUserInfo As DataRow) Public Event OnUserInvalid()
In this event 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
' Logic : This is the Function that will be invoked to check the database 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
' Logic : This Will Execute the specified Stored Procedure with the user id and password parameters
' and return true if user exists and false when not exists
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
The Code for validating the user login if the selection mode is set to Select Query 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() 'if Properties are not filled the application will exit. 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
To Download the Sample Project Please visit http://spaces.msn.com/members/barathan/
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|