Click here to Skip to main content
16,017,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating windows application in Visual Studio 2008 and database SQL 2005. i was creating login ID and password form but before i want create ID form in that when i am going to click on CreateID button ....it has to first check for previous login ID in SQL database Login table...if Login ID is present then it have to give error for example
" Login ID is already present " ....how to write the code for it...

please help me if anyone knows...
Posted

if your Login ID is a a textbox then in the OnClick event of the button:

SQL
string MyCheckQuery = string.Format("SELECT count(0) FROM tblLogin WHERE loginName ='{0}'", textBox1.Text);


That query can be used with an SqlCommand
http://msdn.microsoft.com/en-us/library/877h0y3a.aspx[^]

If the result of the query = 0 then the name is unique, otherwies the result of the query is 1
 
Share this answer
 
Try this:
SQL
IF EXISTS(SELECT LoginID FROM Users WHERE LoginID = @LoginID)
BEGIN
    RAISERROR('LoginID already exists.', 1,1)
END
ELSE
BEGIN
    INSERT INTO Users(..........)
    VALUES (............)
END
 
Share this answer
 
Comments
nishikant.K 9-Aug-11 6:25am    
how to write code in C#.....

please give code in C#
Let the database do it. If you set the loginID to be a unique column, trying to add an id that already exists will generate an error in the stored procedure which will be reflected to the client app as an exception (or maybe the return value from the stored proc will indicate a problem).
 
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