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 8029859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:33:56+00:00 2026-06-05T00:33:56+00:00

I need handle exception duplicate. var resource = new Resource() { url = tbUrl.Text

  • 0

I need handle exception duplicate.

var resource = new Resource() { url = tbUrl.Text };
try
{
    context.Resource.AddObject(resource);
    context.SaveChanges();
}   
catch(UpdateException ex)
{
    // how to know exactly which is why the error occurred
}
catch (Exception ex)
{

    throw;
}

UPDATE:

I need to catch an error that occurs when I try to not add unique value.
UpdateException – is triggered when an error in adding data.

  • 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-06-05T00:33:57+00:00Added an answer on June 5, 2026 at 12:33 am

    This may be a little late, but for anyone with an ASP.NET database update error looking for details, the InnerException error handler is extraordinary helpful and detailed.

            try { 
    
                db.SaveChanges();                              
            }
    
            catch (System.Data.UpdateException ex)    
            {
                Console.WriteLine(ex.InnerException);
            }
    
    
            catch (System.Data.Entity.Infrastructure.DbUpdateException ex) //DbContext
            {
                Console.WriteLine(ex.InnerException);
            }
    
            catch (Exception ex)
            {
    
                Console.WriteLine(ex.InnerException);
                throw;
            }
    

    The T-SQL in the database prevents the insertion of duplicate cities.

    USE [ModelFirstApplication]
    GO
    
    /****** Object:  Index [UQ_Address_City]    Script Date: 5/30/2012 7:26:16 AM ******/
    ALTER TABLE [dbo].[Addresses] ADD  CONSTRAINT [UQ_Address_City] UNIQUE NONCLUSTERED 
    ([City] ASC)
    GO
    

    So, if the user tries inserting “Spokane” a second time into the Addresses table into this demo application, the above exception handler reports

    System.Data.UpdateException: An error occurred while updating the entries. 
    
    See the inner exception for details. ---> 
    
    System.Data.SqlClient.SqlException: Violation of UNIQUE KEY constraint 'UQ_Address_City'. 
    Cannot insert duplicate key in object 'dbo.Addresses'. 
    The duplicate key value is (Spokane).
    The statement has been terminated.
    

    Knowing which exception handler to use is pretty darned handy and your question is quite clear.

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

Sidebar

Related Questions

whenever we need to exception handling, we will surround with try/catch block, but when
i need to identify who raise an exception to handle better str error, is
How can I catch an inner exception in .NET? I need to check 2
I need to handle exception in Application_UnhandledException event of App.xaml.cs file. I am getting
I need to handle orientation (Portrait - Landscape) in my application. Basic layout of
I need to handle the case when a user clicks on a mapTypeControl differently
I need to handle some signal with sigaction, but when I compile my code
I need to handle strings in my php script using regular expressions. But there
For a project I need to handle audio in an iPhone app quite special
I'm using IIS7 and ASP.NET. Basically building a simple CMS and need to handle

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.