Checking database for records before Insertion

Posted on 2005-04-27
Last Modified: 2012-05-05
Hello All.

 Here is what im trying to do in a nut shell(Using VB.NET):

1. Insert a new customer record into the database.
2. If that new record already exist, then display a warning message for the user stating:

"Customer already exist in system".

3. Based on 1 and 2 above, my end result should be one of the following:

Sucessfully insert new customer record. Then display a sucess message:

"The customer has been added!"


Display a warning message about the customer already being in the database:

"Customer already exist in system!"

I hope this is clear.

Thanks everyone for your help!
Question by:Rafiq24
    LVL 9

    Expert Comment

    Are you using stored procedures?  or plain sql command queries?  What field are you using as a unique identifier?  username, email address, etc?

    If you're using SQL Server, s'procs are obviously the best solution for checking for an existing record, but if you're using an Access database or some other database where s'procs arent available, it's still doable with regular sql queries.

    Author Comment

    1. No Im not using store procedures.
    2. Yes Im using Plain sql queries.
    3. Im using an autonumber for my unique identfier
    4. Im using Access 2003

    How can this be done with regular sql queries?
    LVL 9

    Accepted Solution

    When I said "unique identifier" i meant how are you deciding if the user is already in the database?  I didn't mean the primary key.  Sorry if i was unclear.  Assuming you're using an email address, do the following:

    execute this SQL command:

    SELECT UserID FROM tblUsers WHERE emailaddress = '<insert email address here>'

    Check the number of records returned.  If you're using a DataTable just do:

    if(myDataTable.Rows.Count > 0)
        { // user already exists }
        { // execute INSERT command }

    Author Comment

    Do i need to specify to 2  OleDBCommand variables to hold both queries. One for the "Select Query and the other for the "INSERT" Query??
    LVL 9

    Expert Comment

    nope, you can write over top of the "select" command and re-execute it.

    Author Comment

    Cool. This is what Im looking for! Thanks a million.
    LVL 9

    Expert Comment

    no problem.  glad i could help.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    734 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

    23 Experts available now in Live!

    Get 1:1 Help Now