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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:09:14+00:00 2026-06-01T08:09:14+00:00

Sometimes data is deleted from a database by a user which has been referred

  • 0

Sometimes data is deleted from a database by a user which has been referred to. A try catch in razor will handle this just fine, but is there another way?

Current Situation: The model holds a list of Boxes. Each Box has a Container. Safeguarding against Container deletion to avoid errors means using try catch.

No error handling,

<td>@m.Container.Name</td>

Error handling,

<td>@try{@m.Container.Name}catch { Deleted }</td>

Many lists have similar situations, where it is possible that relational data may be removed. Is there a better way to accomplish this aside from this try catch approach?

Some extra code to help with the example:

View Model:

public class BoxVM
{
 List<Box> Boxes { get; set; }
}

Models:

public class Box
{
 public int BoxId { get; set; }
 public Material Material { get; set; }
 public Container Container { get; set; }
}

public class Material
{
 //fk
 public int BoxId { get; set; }
 public string Description { get; set; }
}

public class Container
{
 //fk
 public int BoxId { get; set; }
 public string Name { get; set; }
}

Controller:

public ActionResult showBoxList()
{
 var vm = new BoxVM();
 vm.boxes = _boxRepo.Get().ToList();
 var materials = _materialRepo.Get().ToList();
 var containers = _containerRepo.Get().ToList();
 foreach(var box in vm.boxes)
 {
  box.Material = materials.Where(m => m.BoxId == box.BoxId).Single();
  box.Container = containers.Where(c => c.BoxId == box.BoxId).Single();
 }
 return View(vm);
}

View:

@model BoxVM

foreach(var m in Model.boxes)
{
 <p>@m.Container.Name</p> @*// Breaks if specific container was deleted *@
 <p>@try{@m.Container.Name}catch{<text>Deleted</text>}</p> @*// Doesn't break, but is there a better way? *@
}
  • 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-01T08:09:15+00:00Added an answer on June 1, 2026 at 8:09 am

    Is there a better way to accomplish this aside from this try catch approach?

    Yes. Use an approach which supports a verbose referential integrity policy allowing for the deletion of a single node in an object graph while maintaining the relevance of the object graph.

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

Sidebar

Related Questions

I get data from a textarea where a user has to enter a name
Recently, sometimes (rarely) when we export data from our application, the export log contains
I have an each method that is run on some user-submitted data. Sometimes it
We are using MySQL to get data from database, match the data and send
I have an existing SQL Server database, where I store data from large specific
the program on which I am working gets data from a web service, adds
I sometimes import data from CSV files that were provided to me, into a
I had used the SqlDataSource to retrieve data from database to gridview. <asp:SqlDataSource ID=SqlDataSource1
I have two complicated PHP objects, each of which has data in a few
memset is sometimes used to initialize data in a constructor like the example below.

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.