Click here to Skip to main content
11,935,066 members (56,306 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# SQL-Server ASP.NET4
i need a validation to restrict the entries of duplicate data in my database. i am using, c# and sql server as my development environment.
i have in total four tables in a database called 'CGRT' and table names being project,staff,yearly,monthly . please give me a solution so that i can restrict the entries of duplicate data i would prefer if its at client end with a error msg.
Posted 28-Oct-12 2:50am
Kuthuparakkal 28-Oct-12 7:52am
Have you tried implementing Primary Keys ?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I'm not coding it for you!

The way I would have done it, if resource use was not an issue, and it was 100 or less rows in the tables, was to load the relevant table(s) into a DataSet[^] and search through all rows in the first column (not the record ID column if it has one), if the first fits compare each relavant field on the row using String.Compare() and should all match, then return error message or whatever you want.

Now I use C# for desktop so I don't need to worry to much about resources, but if this is on a website calling another websites SQL server, it's not a good fix.

hope this was helpful!


PS: feel free to comment on this if I'm missing the point of the question entirely
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

The better and most optimized solution is to write a query or stored procedure which returns the count of the existing values if count is not equal to zero means you already have this record in your table skip further insertion.

The term 'Duplicate Entries' only be defined you so you have to modify the query with your parameter a sample query look like this.

eg: select count(staffName) from student where staffName ='Bingo'
this query will return number of items that matches staffName Bingo. If Bingo already exists it will return 0. else the number of occurrence
Rate this: bad
Please Sign up or sign in to vote.

Solution 5


You can use EXISTS for your solution.
SELECT DepartmentID, Name 
FROM HumanResources.Department 

Refer this link for better result.Preventdiuplicate record entry


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151126.1 | Last Updated 14 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100