inserting non duplicate data

Posted on 2006-05-11
Last Modified: 2010-04-16
hi all,

I have an page in which users insert data to the database. my question is that how can i search if the data is a duplicate and if so show a message that says that the data is already in the db. if not just insert the data and show a message that says it is successfully enterd

Question by:jemigossaye
    LVL 35

    Expert Comment

    One way to do this is to have a primary key in the database or a constraint on the table.  Just try to do the insert and catch the constraint violation, if you see that then you can display the message that the data is already entered, otherwise it will have inserted.
    LVL 12

    Expert Comment

    Generally speaking:

    1. compare it to a local dataset of data that matches the data in the database

    2. do a query against the database to see if values user wants to add already exist

    3.  put constraints on the database that don't allow duplicate data, which will cause the insert to fail.  By catching the exeception, you would then know the data already exists.
    LVL 1

    Accepted Solution

    If you want to check if the data already exists you can write a SQL query like

    Select Count(*) from <Table_Name>
    where <column-name1> = ' value1' and <column-name2> = ' value2'

    For Eg.
    The testtable has 2 columns id,name.

    select count(*) from testtable where id = 2 and name = '2'

    Use the command object ExecuteScalar method to get the count of number of rows. If the count is 1, then the row already exists. If not you can then enter the values into the database.

    Author Comment

    hi sirdon,
    how can i count the executescalar()
    what i am doing now is trying to cast it to an int and assign it to an int var
    and then with in if else i am will try to insert it

    LVL 1

    Expert Comment

    hi  jemigossaye,
    Yep that is the way you have to do it. Type cast it to an int and then check the value of the int.

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    745 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

    18 Experts available now in Live!

    Get 1:1 Help Now