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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:21:48+00:00 2026-05-20T11:21:48+00:00

When you have a number of items to insert into DB from a ASP.NET

  • 0

When you have a number of items to insert into DB from a ASP.NET MVC app (like a collection of posted files) do you perform most of the exception handling logic in the controller or inisde the repository?

Do you do this

    [AcceptVerbs(HttpVerbs.Post)]
    public virtual ActionResult Import(HttpPostedFileBase fileToUpload)
    {
        string path = Server.MapPath("~/App_Data/Upload/") + Path.GetFileName(fileToUpload.FileName);
        fileToUpload.SaveAs(path);
        string selectCmd = "SELECT * FROM [Pricelist$]";

        try {
            foreach (DataRow dr in ExcelUtility.ReadFile(path, selectCmd).Tables[0].Rows) {
                Product p = new Product
                {
                    Title = dr["product_name1"].ToString().Trim(),
                    Measure = dr["product_measure"].ToString().Trim(),
                };

                if (dr["product_price"] != DBNull.Value)
                    p.Price = (double)dr["product_price"];
                if (dr["product_taxrate"] != DBNull.Value)
                    p.TaxRate = (double)dr["product_taxrate"];
                _repository.Add(p);
            }
        }
        catch (Exception e) {
            // handle exception or run the action in transaction??
        }

        return RedirectToAction("ImportSuccessful");
    }

or something else (maybe handle the exception in the repository and have repository action return true/false based on whether the method was successfully executed or not)?

  • 1 1 Answer
  • 2 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-20T11:21:49+00:00Added an answer on May 20, 2026 at 11:21 am

    Since my question wasn’t formulated to the best of my abilities because I didn’t actually know what exactly I would like for the answer, I’m going to post the solution that I’ve came up with in this last day.

    First, my repository handles all of the data functions. Second, my repository method return true/false whether the data insertions were successful (there may be problems with inserting into database that are not exceptions, just some business rules failing).
    Third, my repository method builds a report that is returned via output params.

    This is the controller action code now:

        //
        // POST: /admin/post/import
        [HttpPost]
        public virtual ActionResult Import(HttpPostedFileBase fileToUpload)
        {
            string path = Server.MapPath("~/App_Data/Upload/") + Path.GetFileNameWithoutExtension(fileToUpload.FileName) + "-" + Guid.NewGuid() + Path.GetExtension(fileToUpload.FileName);
            fileToUpload.SaveAs(path);
            string report = string.Empty;
            bool success = _repository.ImportPostcodes(path, out report);
            TempData["ImportReport"] = report;
            if (success)
                return RedirectToAction(MVC.admin.Post.ActionNames.ImportSuccess);
            else
                return RedirectToAction(MVC.admin.Post.ActionNames.ImportFailed);
        }
    

    And this is the repository interface:

    public interface IPostCodeRepository
    {
        IApplicationState AppState { get; set; }
        void Add(PostCode instance);
        void Delete(PostCode instance);
        void Delete(Guid rowGuid);
        void Delete(string postalCode);
        IQueryable<PostCode> GetAll();
        IQueryable<PostCode> GetAll(string query);
        PostCode Load(Guid rowGuid);
        PostCode Load(string postalCode);
        void SaveChanges();
        void Update(PostCode instance);
        bool ImportPostcodes(string path, out string report);
    }
    

    I think this is as optimal as it gets..but if someone has a comment on it, be my guest.

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

Sidebar

Related Questions

I am working on an ASP.NET MVC Project and I have a number of
I have a following table using MVC that shows number of items the user
I have jmeter flow like this: ThreadGroup --Sampler to get the number of items
I am trying to insert items from my array into my tree. My function
I have a simple array with x number of items. I am displaying them
I have a windows form with a DropDownList with a fixed number of items.
I currently have about 650,000 items in memcached (430MB memory used) and the number
I'm trying to insert new records into SQLite database from Python code. con =
I have a number of objects. They can be sorted into groups. When a
I would like to programmatically insert additional rows into a DataGrid (to act as

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.