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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:31:18+00:00 2026-05-27T20:31:18+00:00

In my ASP.NET MVC 3 application, I use EF 4.2. In my database, I

  • 0

In my ASP.NET MVC 3 application, I use EF 4.2. In my database, I have a unique constraint for a column.

I try to insert same data in order to see what I get but I am getting the below error:

An error occurred while updating the entries. See the inner exception
for details.

Inside the inner exception I can see the full error about unique constraint. But how can I uniquely catch this exception to tell the user this:

You are entering the same value again.

Here is what I do currently:

try
{
    UpdateModel<ConditionType>(conditionType, null, null, new string[] { "ConditionTypeId" });
    _conditionTypeRepository.Save();

    return RedirectToAction("conditiontype");
}
catch (Exception ex)
{
    ModelState.AddModelError("", "There was an error while updating: " + ex.Message);
}

But this is a generic approach. What I would like to do is to provide a specific message.

Any thoughts?

Edit:

I tired the below but this time it didn’t catch it:

catch (SqlException ex)
{
    if (ex.Number == 2627)
    {
        ModelState.AddModelError("", "You are entering the same value again.");
    }

    ModelState.AddModelError("", "There was an error while updating the value: " + ex.Message);
}

I dug into a little bit and it turned out that it throws an exception type of System.Data.Entity.Infrastructure.DbUpdateException which does not contain the exception number.

EDIT:

Here how I solve the problem but I am sure it is not the best way of solving it. Any idea how to refactor this code?

catch (Exception ex) {

    if (ex.InnerException.InnerException.GetType() == typeof(SqlException)) {

        if (((SqlException)ex.InnerException.InnerException).Number == 2627)
            ModelState.AddModelError("", "You are entering the same value again.");
        else
            ModelState.AddModelError("", "There was an error while updating the value: " + ex.Message);

    } else {
        ModelState.AddModelError("", "There was an error while updating the value: " + ex.Message);
    }
}
  • 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-27T20:31:19+00:00Added an answer on May 27, 2026 at 8:31 pm

    You could do something like this to look for an inner exception that is a SqlException and then handle the sql exception differently.

    catch(Exception ex)
    {
        Exception current = ex;
        SqlException se = null;
        do
        {
            se = current.InnerException as SqlException;
            current = current.InnerException;
        }
        while (current != null && se == null);
    
        if (se != null)
        {
            // Do your SqlException processing here
        }
        else
        {
            // Do other exception processing here
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For my ASP.NET MVC 2 application I use Entity Framework 1.0 as my data
I am about to use linqtosql in my first asp.net mvc application. I have
In C# ASP.NET MVC application I use Link to SQL to provide data for
I have an existing ASP.NET MVC application with some sample data in the SQL
I am developing an asp.net mvc application and have created my custom user database
I have an ASP.NET MVC 3 application and in my project I use ADO.NET
I have a ASP.net MVC application that gets marker coordinates from a database (I
I'm writing an ASP.NET MVC application and trying to use a RESX file to
I am developing an ASP.NET MVC application and I want to use OpenId. What
I'm trying to use route constraints in an Asp.Net MVC Application. routes.MapRoute( theRoute, MyAction/{page},

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.