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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 2, 2013, 9:27 PM EDT
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
Sunny SharmaRank #223 · 11.1K reputationPosted Aug 2, 2013, 9:29 PM EDT
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 :)