M
M
MrLumusss2022-03-21 14:22:33
C++ / C#
MrLumusss, 2022-03-21 14:22:33

How to fix "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection" error?

Good afternoon. There is an inventory program. I open the search form. values ​​and when you click on the search button, the exception "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection" pops up. In this case, the same method works fine in another block of code

Method that throws an exception:

public static void FillItemsList(MaterialListView lvItems, List<Item> items)
        {
            lvItems.Items.Clear();

            List<string[]> data = new List<string[]>();

            foreach (var item in items)
            {
                if (item.DeliveryDate is DateTime)
                {
                    data.Add(new[] {
                        item.id.ToString(),
                        item.Employee1.Name,
                        item.ItemType.Name,
                        item.Name,
                        item.Count.ToString(),
                        item.DateOfIssue.ToShortDateString(),
                        item.DeliveryDate.ToString().Substring(0, 10)
                    });
                }
                else
                {
                    data.Add(new[] {
                        item.id.ToString(),
                        item.Employee1.Name,
                        item.ItemType.Name,
                        item.Name,
                        item.Count.ToString(),
                        item.DateOfIssue.ToShortDateString(),
                        item.DeliveryDate.ToString()
                    });
                }
            }

            var listItems = data.Select(item => new ListViewItem(item)).ToArray();
            lvItems.Items.AddRange(listItems);
        }


Also the methods that are involved in processing the search:
public static List<Item> SearchItem(string name, string type, DateTime dateOfIssue, DateTime? deliveryDate, params int[] cheks)
        {
            using (var db = new InventContext())
            {
                var items = db.Item.ToList();

                foreach (int i in cheks)
                {
                    if (i == 1)
                        items = items.Where(item => item.Name.Contains(name)).ToList();

                    if (i == 2)
                        items = items.Where(item => item.ItemType.Name.Contains(type)).ToList();

                    if (i == 3)
                        items = items.Where(item => item.DateOfIssue == dateOfIssue).ToList();

                    if (i == 4)
                        items = items.Where(item => item.DeliveryDate == deliveryDate).ToList();
                }

                return items;     
            }
        }


Search button click handler:
int[] checks = GetCheckBoxStat();

            if (checks.Length > 0)
            {
                DateTime deliveryDate;
                bool testDeliveryDate = false;

                DateTime dateOfIssue = DateTime.Now;

                if (DateTime.TryParse(txtDeliveryDate.Text, out deliveryDate)) testDeliveryDate = true;
                DateTime.TryParse(txtDateOfIssue.Text, out dateOfIssue);


                if (testDeliveryDate)
                {
                    var items = SearchItem(txtName.Text, txtType.Text, dateOfIssue, deliveryDate, checks);
                    FillItemsList(lvItems, items);
                }
                    
                else
                {
                    var items = SearchItem(txtName.Text, txtType.Text, dateOfIssue, null, checks);
                    FillItemsList(lvItems, items);
                }
                    
            }
            else
            {
                MessageBox.Show("Выберите хотя бы одно условие поиска", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2022-03-21
@MrLumusss

Well, apparently somewhere inside item referred to ObjectContext. Probably for lazy loading.
And you already got him down.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question