Click here to Skip to main content
15,907,225 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Experts !! am working on Asp.net,c#,sqlserver 2005

In my project, i have a Registration Form. and also i have Registration Table in database.
So i need to create a Usernames as Unique.

Suppose if the User Registered with one name and the other users should not create with this name.

So , i need a Message like Already Exists with this Name, Try another name.


Please Suggest me, and help me, How to do this ?

Thanks.
Posted
Comments
Rahul Rajat Singh 29-May-12 0:22am    
The answer that you have marked as solution seems to be perfect. but I would like to suggest that you do it on client side using XMLHttpRequest or MS Ajax stuff so that the web page you are making will be more responsive.

You can use TexboxChanged Event and make textbox property Autopostback True.

now in Textbox changed Event :


C#
 string pass="select username from tablename where username='"+txt_username.Text+"'";
SqlDataAdapter ad = new SqlDataAdapter(pass,connection object);
DataSet ds = new DataSet();
ad.Fill(ds);
if (ds.Tables[0].Rows.Count != 0)
{
            //Means Already Exits....
         txt_username.Focus();
//Display Message like Already Exits...
}
else
{
           // Means New USer
}



Enjoyyyy
Mitesh
 
Share this answer
 
v2
Comments
hitech_s 26-May-12 4:07am    
my 5!
hey


you can do this in your stored procedure simply

try this

SQL
as
begin
if exists(select user_Name from Tbl_name where UserName= @userName )
begin
select 0
end
else
begin
-- Here write your insert query ---
end 
begin
select 1
end



after writing this take a lable near your text box and show your message.
Hope this will help you.
 
Share this answer
 
v2
In my opinion the best way to handle this is to define the user name as unique in the database. This way the database would generate and error (which you can then catch as an exception) if the username already exists.

See: SQL UNIQUE Constraint[^] and UNIQUE Constraints[^]
 
Share this answer
 
C#
string duplicate = "select uname from table name where uname = '"+txtuname.text+"'";
SqlDataAdapter da = new SqlDataAdapter(pass,con);
DataSet ds = new DataSet();
da.fill(ds);
if (ds.Tables[0].Rows.Count != 0)
{
          //***Already Exits**//
          response.write("<script>alert('Already Exits')</script>");
          txtuname.focus();
}
else
{
          //*****new user code***//
          //insert qurey here
}


if you have any query fill free to contact
Thank You
 
Share this answer
 
v2

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