Solved

Check for duplicate in c# and sql

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

729 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