-1

I'm trying to copy data row by row from ProductStatisticsTemptable to ProductStatistics table.

    [HttpGet]
    public ActionResult Copy_DatatoProductStatistics()
    {
        string errormsg = "";
        //var incomplete_product_result = db.Database.SqlQuery<ProductStatistics>("Copy_ProductStatistics");
        var str = from a in db.ProductStatisticsTemp select a;

        ProductStatistics ls = new ProductStatistics();

        try
        {
            foreach (var val in str) // iterator the data from the list and insert them into the listSecond
            {
                var product = db.Product.Find(val.Product_ID);

                try
                {

                    if (product.ProductID == val.Product_ID & product.ProductTitleEn == val.ProductNameEn & product.ProductTitleAr == val.ProductNameAr)
                    {
                        try
                        {

                            if (!String.IsNullOrEmpty(val.SalesVolumes) | !String.IsNullOrEmpty(val.Target) | !String.IsNullOrEmpty(val.Profitability) | !String.IsNullOrEmpty(val.AnnualGrowthRate))
                            {
                                ls.Product_ID = val.Product_ID;
                                ls.ProductNameEn = val.ProductNameEn;
                                ls.ProductNameAr = val.ProductNameAr;
                                ls.Profitability = val.Profitability;
                                ls.Target = val.Target;
                                ls.SalesVolumes = val.SalesVolumes;
                                ls.Subsidary_ID = val.Subsidary_ID;
                                ls.AnnualGrowthRate = val.AnnualGrowthRate;

                                db.ProductStatistics.Add(ls);

                            }

                               db.SaveChanges();

                        }

                        catch (Exception ex)
                        {
                            errormsg = ex.Message;
                        }


                    }

                }
                catch (Exception ex)
                {
                    errormsg = "Product ID or Product Name Doesnt match with existing details";
                }

            }           


        }

        catch (Exception ex)
        {
            errormsg = ex.Message;                
        }


        return RedirectToAction("FileUpload", "FileUpload", new { error = errormsg });

    }

but after db.SaveChanges(); above line its directing to catch (Exception ex) . once I debug I can see following inner exception message

Message = "New transaction is not allowed because there are other threads running in the session."

kez
  • 2,273
  • 9
  • 64
  • 123
  • Never ever call you database code from controller code ...Create a Data Access Layer which you should be calling from controller – Viru Mar 21 '16 at 06:18
  • If your google that error message and read the first few hits you will understand how to solve it. –  Mar 21 '16 at 06:23

1 Answers1

0

Try to use:

IList<ProductStatistics> str = from a db.ProductStatisticsTemp select a;
foreach (var val in str)
{

    // your code
}

You need to call EF and try to return it into an IList<T> of that target type and then we can use a loop on the IList<T>

You can also check this related thread

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331