Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6570987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:49:32+00:00 2026-05-25T14:49:32+00:00

Usually I have to insert some data in a DB and it can’t be

  • 0

Usually I have to insert some data in a DB and it can’t be inserted because the table has constraints preventing me from doing that. With the app I’m developing, some business rules (like “there are not two persons with the same id type and number” or “the XXXX product is already registered”) are enforced with UNIQUE or composite keys and other mechanisms. Although I know that DBMS throws an error message (like ORA-6346 or ) I do not know how to catch those errors in .net 4.0 and translate them to an error that can be meaningful for the business layer.

As an example: I’ve seen an insertion mechanism that asks the DB if the register already exists and then it proceeds to insert data if it isn’t the case. I want to do this only using a query and catching the database constraint violation error because the first way seems to me as very inefficient (DB can alert you about duplication with an error).

How can I implement something like that?

Note: I think that it is possible to catch the exception from the database and use its ORA-xxxx code to try to figure out what has happened. I do not remember with precision if the error message shows which constraint (the name of…) has been broken, but business layer code can contain constants with the constraint names and, from them, know what has happened.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:49:32+00:00Added an answer on May 25, 2026 at 2:49 pm

    What you should do here depends really on the architecture of your system, and your attitude towards the placement of business logic.

    Many systems architects prefer to use a database as a dumb data store, and implement the type of error handling and integrity checking you’re talking about in the middle/application layer. This is a perfectly valid approach, and is particularly suited to systems requiring regular smaller releases, where the business logic is subject to regular change (its much easier to redistribute an executable midweek than to co-ordinate a database release), and where the data model is fairly simple.

    The other approach is to put some well-defined semi-permanent business logic into the database layer. This is particularly powerful when the data model is more complex, and you have a good DBA! 😉

    My personal opinion is, an enterprise database should be responsible for its own integrity, and so I prefer to have logic in the database layer to ensure this – removing any vulnerability to bugs being introduced in non-database code releases. So in your specific example, I would definitely catch the error and report it meaningfully to your application layer.

    Oracle supports catching various types of error using names exceptions, allowing you to raise these exceptions to your applications in a meaningful way. For example:

    PROCEDURE test() AS
      b VARCHAR2;
    BEGIN
    
      -- if the following row exists, then DUP_VAL_ON_INDEX will be thrown 
      -- (assuming there is a primary key constraint)        
    
      INSERT INTO table(a,b,c)
      VALUES(1,2,3);  
    
      -- if there is no matching record, NO_DATA_FOUND will be thrown
    
      SELECT a
      INTO b
      FROM TABLE
      WHERE c = 'blah';  
    
    EXCEPTION   -- both types of exception can be caught and embellished 
      WHEN DUP_VAL_ON_INDEX THEN
        raise_application_error(-20570, 'Attempted to insert a duplicate value', TRUE);
      WHEN NO_DATA_FOUND THEN
        raise_application_error(-20571, 'No matching row in table for value:' || 'blah', TRUE);
      WHEN OTHERS THEN
      rollback
    END test;
    

    You can find more information here: http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/errors.htm

    Hope this helps..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with a VARBINARY(MAX) field (SQL Server 2008 with FILESTREAM )
I have a script that compiles a list of items that can be very
I need to create a dbf file which has the table structure of an
I know this is probably one of those you just have to try it
This is probably stupid simple, but for some reason I'm having trouble getting it
I have just found these performance notes for cPython lists : Time needed for
I am using PHP to send emails on demand to clients. I have a
So i've just begun working with SVGGraph http://www.goat1000.com/svggraph.php .. I'm quite happy with it
I recently ran into this problem: Rails 3 link_to (:method => :delete) not working
We're having mysterious network problems with MySQL. Simple update query (updating single row using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.