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

  • Home
  • SEARCH
  • 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 8173345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:06:51+00:00 2026-06-06T22:06:51+00:00

I am developing an asp.net mvc application where user1 could delete data records which

  • 0

I am developing an asp.net mvc application where user1 could delete data records which were just loaded before by user2. User2 either changes this non-existent data record (Update) or is doing an insert with this data in another table that a foreign-key constraint is violated.

Where do you catch such expected exceptions?

In the Controller of your asp.net mvc application or in the business service?

Just a sidenote: I only catch the SqlException here if its a ForeignKey constraint exception to tell the user that another user has deleted a certain parent record and therefore he can not create the testplan. But this code is not fully implemented yet!

Controller:

  public JsonResult CreateTestplan(Testplan testplan)
  {
   bool success = false;
   string error = string.Empty;

   try
  {
   success = testplanService.CreateTestplan(testplan);
   }
  catch (SqlException ex)
   {
   error = ex.Message;
   }
   return Json(new { success = success, error = error }, JsonRequestBehavior.AllowGet);
  }

OR

Business service:

public Result CreateTestplan(Testplan testplan)
        {
            Result result = new Result();
            try
            {
                using (var con = new SqlConnection(_connectionString))
                using (var trans = new TransactionScope())
                {
                    con.Open();

                    _testplanDataProvider.AddTestplan(testplan);
                    _testplanDataProvider.CreateTeststepsForTestplan(testplan.Id, testplan.TemplateId);
                    trans.Complete();
                    result.Success = true;
                }
            }
            catch (SqlException e)
            {
                result.Error = e.Message;
            }
            return result;
        }

then in the Controller:

public JsonResult CreateTestplan(Testplan testplan)
      {
       Result result = testplanService.CreateTestplan(testplan);      
       return Json(new { success = result.success, error = result.error }, JsonRequestBehavior.AllowGet);
      }
  • 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-06T22:06:55+00:00Added an answer on June 6, 2026 at 10:06 pm

    Foreign key constraint violation should be checked and displayed properly. You can easily check if rows in related table exist and show proper message. The same can be done with row updates. Servers return number of rows affected, so you know what happens.

    Even if you don’t make these checks, you should catch SQL exceptions. For average application user, message about constraint violation means nothing. This message is for developer and you should log it with ELMAH or Log4Net library. User should see message similar to “We are sorry. This row has been probably modified by another user and your operation has become invalid.” and in case he asks developer about it, developer should check logs and see the cause.

    EDIT

    I believe you should check errors in service. Controller should not be aware of data access layer. For controller, it doesn’t matter if you store data in SQL database or in files. Files can throw file access exception, SQL has other ones. Controller shouldn’t worry about it. You can catch data access layer exceptions in service and throw exception with type dedicated for service layer. Controller can catch it and display proper message. So the answer is:

    public class BusinessService 
    {
        public Result CreateTestplan(Testplan testplan)
        {
            Result result = new Result();
            try
            {
                using (var con = new SqlConnection(_connectionString))
                using (var trans = new TransactionScope())
                {
                    con.Open();
    
                    _testplanDataProvider.AddTestplan(testplan);
                    _testplanDataProvider.CreateTeststepsForTestplan(testplan.Id, testplan.TemplateId);
                    trans.Complete();
                    result.Success = true;
                }
            }
            catch (SqlException e)
            {
                ....log in ELMAH or Log4Net using other logging framework...
                throw new ServiceException("We are sorry. Your operation conflicted with another operation in database. It has been cancelled.");
            }
            return result;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing an asp.net mvc application, which has these enity classes: public class
I am developing an asp.net mvc application in which I keep user credentials in
I'm developing a ASP.NET MVC application with 3 layer classic architecture 1. data access
I'm currently developing an asp.net mvc 2 application which uses the default SqlMembershipProvider for
I am developing an application which is web-based (asp.net MVC). The user configures reminders
I am developing a data-driven web application using ASP.Net MVC 3. I will be
I'm developing an ASP.NET MVC 3 application and it uses a complex model which
I'm new to the jQuery and ASP.Net MVC. I'm developing a mvc application which
I'm developing an ASP.Net mvc application which has a requirement to do some small,
I'm developing an ASP.NET MVC application in which I want to encrypt a short

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.