Solved

Check for duplicate in c# and sql

Posted on 2012-03-26
5
813 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

636 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