Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Day Guys


I am sitting with an issue that I am struggling to solve my code works for a while andx then it breaks and I get the following error . The operation cannot be completed because the DbContext has been disposed. Below is my code what Am I doing wrong. Any help would be appreciated.

List<lib.imagemaster> imageList = new List<lib.imagemaster>();
       public const string KEY_FIELD_ReferenceNumber = "ReferenceNumber";
       public const string KEY_FIELD_ImageNumber = "ImageNumber";
       public const string KEY_FIELD_IsIndexed = "IsIndexed";

       public MainForm()
       {
           InitializeComponent();
       }

       private void btnGetIndexingFaults_Click(object sender, EventArgs e)
       {
           imageList = new List<lib.imagemaster>();
           dgvImageDetails.Columns.Clear();
           try
           {
               using (DocumentImagingDB db = new DocumentImagingDB())
               {
                   // Get Images for the previous 2 days
                   DateTime fromDate = DateTime.Now.AddDays(Properties.Settings.Default.DaysToCheck);
 //////// this Line  is where it breaks                 var images = db.Images.Where(i =&gt; i.SubmitDateTime &gt;=        fromDate).ToList();
                   Application.DoEvents();
                   if (images.Count() &gt; 0)
                   {
                       IntegrationAccessLayer.AccessLayerService ALS = new IntegrationAccessLayer.AccessLayerService();
                       // Check each image against AS400
                       images.ToList().ForEach(i =&gt;
                       {
                           // Check "IsIndexed" from Metadata
                           List<integrationaccesslayer.keyvalue> keyValue = new List<integrationaccesslayer.keyvalue>();
                           string[] strArr = new string[0];

                           keyValue.Add(new IntegrationAccessLayer.KeyValue { Key = KEY_FIELD_ImageNumber, Value = i.ImageNumber });
                           IntegrationAccessLayer.ImageDetails[] imageDetails = ALS.GetImageDetails(keyValue.ToArray(), strArr);

                           bool isIndexed = false;

                           imageDetails[0].metaData.ToList().ForEach(md =&gt;
                           {
                               if (md.Key == KEY_FIELD_IsIndexed && md.Value == "true")
                               {
                                   isIndexed = true;
                               }
                           });
                           Application.DoEvents();
                           if (isIndexed)
                           {
                               lib.ImageMaster image = lib.TapeHelper.DoRequest<lib.imagemaster>("Document", "ws_getimagemaster", new lib.ImageNumber { imageNumber = i.ImageNumber });

                               if (image != null && image.IMAGENUMBER != "")
                               {
                                   // Check if BDZ exists.
                                   string indexedPath = db.ImagingSettings.FirstOrDefault().AS400URI.Replace("%YYYY", DateTime.Now.Year.ToString());
                                   indexedPath = indexedPath.Replace("MM%", DateTime.Now.Month.ToString().PadLeft(2, '0'));

                                   string pathURI = indexedPath + "\\" + i.ImageNumber + ".BDZ";

                                   if (!File.Exists(pathURI))
                                   {
                                       // Add Image to List
                                       imageList.Add(image);
                                       txtReindexCount.Text = imageList.Count.ToString();
                                       Application.DoEvents();
                                   }
                               }
                           }

                       });
                       txtReindexCount.Text = imageList.Count.ToString();
                       dgvImageDetails.DataSource = imageList;
                       Application.DoEvents();

                   }
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show("Oops...it broke", "Aaaaahhhhh!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
           }
       }



Kind regards
Posted
Updated 8-Oct-15 23:10pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900