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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:02:36+00:00 2026-05-23T09:02:36+00:00

My issue is the following: i’m trying to build a function to which i

  • 0

My issue is the following: i’m trying to build a function to which i could pass a list of items, which would then go to the db with each of those items and update them. I believe the issue is within the way datacontexts are being used but i cannot figure out this issue.

Here is my function that builds the list of items that were changed:

 protected void btnSave_Click(object sender, EventArgs e)
{
    List<AFF_CMS_FMA> fmasToSave = new List<AFF_CMS_FMA>();
    AFF_CMS_FMA newFmaItem = new AFF_CMS_FMA();

    foreach (AFF_CMS_FMA fmaItem in FmaLib.fetchAllActiveAssetsInFMA())
    {
        if (fmaItem.SortOrder != Convert.ToInt32(Request.Form["fmaItem_" + fmaItem.ID + "_SortOrder"]))
        {
            newFmaItem = fmaItem;
            newFmaItem.Name = SecurityLib.SqlSafeString(Request.Form["fmaItem_" + fmaItem.ID + "_Name"]);
            newFmaItem.AssetID = Convert.ToInt32(Request.Form["fmaItem_" + fmaItem.ID + "_AssetID"]);
            newFmaItem.SortOrder = Convert.ToInt32(Request.Form["fmaItem_" + fmaItem.ID + "_SortOrder"]);
            newFmaItem.ImagePathEn = SecurityLib.SqlSafeString(Request.Form["fmaItem_" + fmaItem.ID + "_ImagePathEn"]);
            newFmaItem.ImagePathCh = SecurityLib.SqlSafeString(Request.Form["fmaItem_" + fmaItem.ID + "_ImagePathCh"]);
            newFmaItem.StartDate = DateTime.Parse(SecurityLib.SqlSafeString(Request.Form["fmaItem_" + fmaItem.ID + "_StartDate"]));
            newFmaItem.EndDate = DateTime.Parse(SecurityLib.SqlSafeString(Request.Form["fmaItem_" + fmaItem.ID + "_EndDate"]));
            newFmaItem.ClickToUrl = SecurityLib.SqlSafeString(Request.Form["fmaItem_" + fmaItem.ID + "_ClickToUrl"]);
            fmasToSave.Add(newFmaItem);

        }
    }
    FmaLib.saveEditedFmas(fmasToSave);
}

here is the function that the foreach loops calls to get all the items that are in the db:

 public static List<AFF_CMS_FMA> fetchAllActiveAssetsInFMA()
    {
        List<AFF_CMS_FMA> results = null;

        using (fmaDataContext db = new fmaDataContext())
        {
            using (TransactionScope ts = new TransactionScope())
            {
                try
                {
                    if (HttpContext.Current.Cache["fmaActiveList"] == null)
                    {
                        db.LoadOptions = loadAll;
                        results = clsCompiledQuery.getAllActiveFmas(db).ToList();
                        HttpContext.Current.Cache["fmaActiveList"] = results;
                    }
                    else
                        results = (List<AFF_CMS_FMA>)HttpContext.Current.Cache["fmaActiveList"];

                    ts.Complete();
                }
                catch (Exception ex)
                { Transaction.Current.Rollback(); }
            }
            return results;
        }
    }

here are the queries being used:

 protected static class clsCompiledQuery
    {
        public static Func<DataContext, IOrderedQueryable<AFF_CMS_FMA>>
        getAllActiveFmas = CompiledQuery.Compile((DataContext db)
        => from fma in db.GetTable<AFF_CMS_FMA>()
           where fma.IsArchived == false
           orderby fma.SortOrder ascending
           select fma);


        public static Func<DataContext, int,IQueryable<AFF_CMS_FMA>>
        getFmaById = CompiledQuery.Compile((DataContext db, int ID)
        => from fma in db.GetTable<AFF_CMS_FMA>()
           where fma.ID == ID
           select fma);

    }

and finally this were im trying to get the save to happen to the db but no exeptions are throwns, yet the db does not change

 public static bool saveEditedFmas(List<AFF_CMS_FMA> fmaToSaveList)
    {
        using (fmaDataContext db = new fmaDataContext())
        {
            using (TransactionScope ts = new TransactionScope())
            {
                try
                {
                    foreach (AFF_CMS_FMA fmaItemToSave in fmaToSaveList)
                    {
                        AFF_CMS_FMA fmaItemToUpdate = clsCompiledQuery.getFmaById(db, fmaItemToSave.ID).ToList()[0];
                        fmaItemToUpdate = fmaItemToSave;

                        db.SubmitChanges();
                    }

                    return true;
                }
                catch (Exception ex)
                {
                    Transaction.Current.Rollback();
                    return false;
                }
            }
        }
    }

I have checked and the table does contain a primary key in the designer. If i do the save from the btnSave_click function by passing a datacontext to the fetchAllActiveAssetsInFMA() then doing submitchanges on that context it works .. but im trying to abstract that from there.

thanks all in advance

  • 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-23T09:02:37+00:00Added an answer on May 23, 2026 at 9:02 am

    Your not calling ts.Complete in function saveEditedFmas.

    Also I would recommend calling db.SubmitChanges(); outside of the for loop. And why do you have a transaction in function fetchAllActiveAssetsInFMA? It’s only fetching data right? And I’m not quite sure whats happening inside the for loop in save function, looks strange.

    I think you should map the properties from fmaItemToSave to fmaItemToUpdate

    foreach (var fmaItemToSave in fmaToSaveList)
    {
       var fmaItemToUpdate = clsCompiledQuery.getFmaById(db, fmaItemToSave.ID).First();
       fmaItemToUpdate.Name    = fmaItemToSave.Name;
       fmaItemToUpdate.AssetID = fmaItemToSave.AssetID;
       //And the rest of the properties           
    }
    db.SubmitChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following issue: I'm generating a Top10 list of items, based on
I have the following issue and I would like to know what exactly happens.
I'm having the following issue in a WinForms app. I'm trying to implement Hotkeys
I have the following issue; Here is my string I'm trying to remove javascript:l(
I have the following issue: I want to create a method which accepts input
I have the following issue: I'm trying to deploy my project on heroku but
I have the following issue with a Debug.Assert ine in a function public override
I have the following issue with my local function. The following function: declare function
I have the following issue, Im creating a windows 7 gadget, which uses Iframe
I have the following issue. On load I want to use each to go

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.