Avoid inserting duplicate records in c# [duplicate]
How to avoid inserting duplicate records in c#
For exampl开发者_JAVA技巧e:
Table name: record1 in that userName is primary key
- insert record1.(userName=101, password=raj, confirmPassword=raj)
- insert record2.(userName=102, password=kumar, confirmPassword=kumar)
- insert record3.(userName=101, password=jose, confirmPassword=jose)
When the ok button is clicked it shows a message like
MessageBox.Show("User name already inserted. Please change username");
Create an index on the table on the userName column and make it Unique. This will throw an exception when a duplicate record is added.
Are you familiar with using a database system? Maybe I'm misunderstanding you, but all you would do is run a query to see if the user exists or check on the SQL error returned if you have a constraint on the username.
If that is indeed what you were looking for, I think you should learn to use Google more often and learn SQL before diving into writing a system that requires it. :)
You could catch the primary key violation and handle it with your error message.
- Validate in your usertable (via a query) that the username does not exist yet
- If not, insert, else show the messagebox
精彩评论