Solved

Check for duplicate in c# and sql

Posted on 2012-03-26
5
800 Views
Last Modified: 2012-07-16
We have a currently c#/asp web app using a sql database, i'm trying to find a way to achieve the following...

When the user adds a new part to the database it checks to see if it exists before adding it, if it does find a duplicate it triggers a warning (ideally with some information on what part it has found as a conflict)

I'd like the warning to contain some basic details about the part that already exists if possible and for it to be triggered when the user clicks the save button.

Attached is the page in question (with code behind) from Control\Quotations\QuotationInfo
And an overview of the project to allow context
quotinfo.zip
Control.zip
0
Comment
Question by:suityou69
  • 2
5 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37767759
apply a unique constraint to check for duplicate data. trying to insert the duplicate data from ADO.NET would result in an exception which can be caught and alert can be shown to the user as given below:-

http://dustyreagan.com/how-to-apply-unique-constraints-to/
http://stackoverflow.com/questions/628189/catching-sql-unique-key-exceptions-in-net
0
 
LVL 1

Author Comment

by:suityou69
ID: 37770121
Thanks for the links, after having a quick read im keen to explore the option of querying if the id already exists as opposed to catch the exception.

Any pointers would be appreciated

Thanks in advance
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37770132
in your procedure just write this:-

if exists(select id from yourtable where id='1234')
begin
return -1;
end
else
begin
your insertion here....
end

Return value can be read from procedure by adding the return parameter in sql parameters.
Here is an example:-
http://geekswithblogs.net/NewThingsILearned/archive/2008/09/17/get-return-value-from-stored-procedure.aspx
http://www.codeproject.com/Questions/71698/how-to-read-the-return-value-of-sql-store-procedur
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now