Click here to Skip to main content
15,996,573 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

There are 4 columns in Sql DB Username varchar(20),Password varchar(20),NoOfFailedAttempts tinyint,Islocked bit not null default 0.When the User enters his credentials i need to verify the credentials in the DB and if the credentials exists the user has to login.If the Username and Pwd are not matching then the NoofFailedAttempts has to be updated and if it reaches 3 Islocked column has to be updated to 1 .
At the same time in the front end i need to show the user the count of login attempts failed.Suppose if the User successfully logged in the 3 rd attempt again the NoofFailedAttempts column has to be set to 0.

Can any one please provide a stored procedure for the above requirement.
Posted

You should use a procedure like this:
SQL
CREATE PROCEDURE spUserLogin
	@Username VARCHAR(50),
	@Password VARCHAR(50)
AS
BEGIN
	IF(SELECT COUNT(*) FROM tblUser WHERE Username = @Username AND Password = @Password) > 0
	BEGIN 
		--Login successfull here
	END
	ELSE
	BEGIN
		DECLARE @NoOfFailedAttempts SMALLINT
		SET @NoOfFailedAttempts = (SELECT NoOfFailedAttempts FROM tblUser WHERE Username = @Username)
		IF @NoOfFailedAttempts = 3
		BEGIN
			 UPDATE tblUser SET Islocked = 1 WHERE Username = @Username
		END
		ELSE
		BEGIN
			 UPDATE tblUser SET NoOfFailedAttempts = @NoOfFailedAttempts + 1 WHERE Username = @Username
		END 
	END
        --Return the number of attempts
        SELECT NoOfFailedAttempts FROM tblUser WHERE Username = @Username
END



--Amit
 
Share this answer
 
v2
Comments
Prathap Gangireddy 13-Sep-12 3:00am    
Hi Amy,

Thank you.

Can you also let me know how to retrieve the NoofFailedAttempts in the front end..so that if the user exists in the database i will let him know the count of failed attempts or else if the user doesn't exist just leave a message that username/password are incorrect.
_Amy 13-Sep-12 3:04am    
Add a select command at the end of the procedure. See my updated answer.
Prathap Gangireddy 13-Sep-12 10:48am    
Thank you..Amy.. I will try this out and let you know.
Look at this if you do not want use existing Membership provider:
http://msdn.microsoft.com/en-us/library/aa478962.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900