i want to just check user name exist in the database using vb.net
For Ex:- In databse Admin is username and suppose another user create account and he gives username as Admin then shows message username already eexists.
Thanks in advance...
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sunny SharmaPosted Aug 3, 2013, 1:29 AM
It's a very simple logic. Just check the if it returns any row by that username. Like,
Select count(username) from myTable where username='Admin';
if it returns greater than 0 then username is already in there.
you can also do like:
Select * from myTable where username='Admin';
and check if it returns any row. If yes, then username exists otherwise not.
Hope you got the point.
Happy Coding :)
Iftikar HussainPosted Aug 3, 2013, 1:27 AM
You can check by using sql query before creating a new account
in your SP where you are creating new user do like this
CREATE PROCEDURE usp_CreateUser
(
@UserName varchar(20)
)
AS
DECLARE @IsUserExist bit
IF EXISTS(SELECT UserName FROM UserTable WHERE UserName=@UserName)
BEGIN
SET @IsUserExist=1
END
ELSE
INSERT INTO UserTable
.........
SET @IsUserExist=0
BEGIN
SELECT @IsUserExist As IsUserExist
in your code use cmd.ExecuteScalar() instead of cmd.ExecuteNonquery()
like this
Dim result as Boolean
Dim message as String
result=Convert.ToBoolean(cmd.ExecuteScalar())
Regards,
Iftikar