Hi guys am using MVC3 and will like to know how to stop the same primary key to be entered within my application. I have my primary key set as NVARCHAR(100) and it can allow the console names I need a reg x of simlar validation to prevent users from entering the same name again thanks
If you need more info please ask
As I have recently answered, I recommend against using varchar() and nvarchar() columns as the primary key. They are slower on joins and QueryById() operations, and the temptation is to allow editing the value (which I also recommend against). Editing primary keys is bad because there is a clustered index on the column. If you change the values enough, you will skew the index and have to rebuild.
It is better to use an IDENTITY column (SQL Server) or SEQUENCE (Oracle) and set a UNIQUE constraint on the Name column. With the unique constraint, the database engine will throw an exception if the user tries to enter a duplicate name. Your code should catch the exception and present an appropriate error message to the user.
I don't think you need a regex for that. Before adding the new record to the database, just perform a select
and check if it is already there.
And like mgnoonan states, using varchars as primary keys is not generally good practice. Every time I've ever come across it, it eventually caused problems.