Solved

Check for duplicate in c# and sql

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

896 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

12 Experts available now in Live!

Get 1:1 Help Now