Solved

Check for duplicate in c# and sql

Posted on 2012-03-26
5
804 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

831 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