Violation of UNIQUE KEY constraint 'UserName'. Cannot insert duplicate key in object 'dbo.Customers'

teardropzerone

New Member
Help! I set up my database in microsoft sql server 2005.<br />
I have this table named Customers<br />
Now i have this field Username<br />
then from modify tables I right click row Username<br />
and select Indexes/keys<br />
then I select add. select columns and select the field Username<br />
and change the Is Unique to Yes and Type to Unique KEy<br />
<br />
My Problem is in registration form of my website this message appears every time I enter username that is already existing in the database. Help me How will I fix it. I just need to know the codes in c# so that the if I enter the existing username it will not be accepted and there will be a message error that the username is already taken. Just like in signing up in yahoo. please help me.. Im using C#, microsoft web developer 2005 and microsoft sql server 2005 <br />
<br />
 

Quandiraw

New Member
You better post the error message. But try these things |)
check the sql statement in your c# code
check the table definition for duplicate keys (Primary, and additional constraint key), etc
 

TheMadProfessor

New Member
You need to trap the error returning from your database engine, most likely thru a Try...Catch block (or whatever the C# equivalent might be). Then check to see if SQL Server has tossed the duplicate key error and handle it appropriately. Any other error, toss it back as an unhandled error.
 
Top