ObjectContext实例已被处置,无法再使用,发生错误

时间:2018-12-10 10:17:56

标签: c# winforms custom-controls

我有一个Windows窗体应用程序。我有一个基本窗口,我正在尝试使用datagridview打开一个查找窗口。我正在打开一个自定义窗体控件,如以下代码所示。然后将所选的值读取为另一种形式的变量。

if (e.KeyCode == Keys.F3)
                {
                    using (DataControllers.RIT_Allocation_Entities RAEntity = new DataControllers.RIT_Allocation_Entities())
                    {
                        lookupGridSourceofJheader = RAEntity.JOB_Header.ToList<DataControllers.JOB_Header>();
                    }

                    var btnOk = new Button() { Text = "Ok", Anchor = AnchorStyles.None };
                    var btnCancel = new Button() { Text = "Cancel", Anchor = AnchorStyles.Right };

                    var dg = new DataGridView();
                    var bs = new BindingSource();

                    bs.DataSource = lookupGridSourceofJheader;
                    dg.DataSource = bs;
                    dg.Dock = DockStyle.Fill;
                    dg.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

                    //setup a layout which will nicely fit to the window
                    var layout = new TableLayoutPanel();
                    layout.Controls.Add(dg, 0, 0);
                    layout.SetColumnSpan(dg, 2);
                    layout.Controls.Add(btnCancel, 1, 1);
                    layout.Controls.Add(btnOk, 0, 1);
                    layout.RowStyles.Add(new RowStyle(SizeType.Percent));
                    layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                    layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent));
                    layout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
                    layout.Dock = DockStyle.Fill;

                    //create a new window and add the cotnrols
                    var window = new Form();
                    window.StartPosition = FormStartPosition.CenterScreen;
                    window.Controls.Add(layout);

                    // set the ok and cancel buttons of the window
                    window.AcceptButton = btnOk;
                    window.CancelButton = btnCancel;
                    btnOk.Click += (s, ev) => { window.DialogResult = DialogResult.OK; };
                    btnCancel.Click += (s, ev) => { window.DialogResult = DialogResult.Cancel; };

                    if (window.ShowDialog() == DialogResult.OK)
                    {
                        selectedJobheader = (DataControllers.JOB_Header)bs.Current;
                        txtJID.Text = selectedJobheader.JobID.ToString();
                        txtCustomer.Text = selectedJobheader.CustomerCode.ToString();
                        txtCustomerName.Text = selectedJobheader.CustomerName.ToString();
                        txtRemarks.Text = selectedJobheader.Remarks.ToString();
                        if (selectedJobheader.Status)
                        {
                            txtStatus.Text = "Pending";
                        }
                        else
                        {
                            txtStatus.Text = "Done";
                        }

                        dtpDate.Value = selectedJobheader.JobDate;
                        lblCustomerTelephone.Text = selectedJobheader.MobileNo.ToString();
                        lblCusLocation.Text = selectedJobheader.LocationCode.ToString();
                        populateJdetailsDatagrid();

                    }
                }

当我尝试滚动出现以下错误的Datagrid时。

  

--------------------------- DataGridView默认错误对话框   --------------------------- DataGridView中发生以下异常:

     

System.Reflection.TargetInvocationException:属性访问器   对象上的“ Job_Details”   'System.Data.Entity.DynamicProxies.TBLM_PRODUCT_5D1C9C0350626BB4D225E83C26CEC12F74C76AE392AE2AD916BEFD258BCBEF2A'   引发以下异常:'ObjectContext实例已被   已废弃,不能再用于需要   连接。' ---> System.ObjectDisposedException:ObjectContext   实例已被处置,不能再用于操作   需要连接。

     

在System.Data.Entity.Core.Objects.ObjectContext.get_Connection()

     在

  System.Data.Entity.Core.Objects.ObjectQuery 1.GetResults(Nullable 1   forMergeOption)

     在

  System.Data.Entity.Core.Objects.ObjectQuery`1.Execute(MergeOption   mergeOption)

     在

  System.Data.Entity.Core.Objects.DataClasses.EntityCollection 1.Load(List 1   集合,MergeOption mergeOption)

     在

  System.Data.Entity.Core.Objects.DataClasses.EntityCollection`1.Load(MergeOption   mergeOption)

     

在System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.Load()

     在

  System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.DeferredLoad()

     在

  System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.LoadProperty [TItem](TItem   propertyValue,字符串关系名称,字符串targetRoleName,布尔值   mustBeNull,对象wrapperObject)

     在

  System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior。<> c__DisplayClass7`2.b__1(TProxy   代理,TItem项)

     在

  System.Data.Entity.DynamicProxies.TBLM_PRODUCT_5D1C9C0350626BB4D225E83C26CEC12F74C76AE392AE2AD916BEFD258BCBEF2A.get_Job_Details()

     

---内部异常堆栈跟踪的结尾---

     

在System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object   组件)

     在

  System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetValue(Int32   boundColumnIndex,Int32 columnIndex,Int32 rowIndex)

     

要替换此默认对话框,请处理DataError事件。

     

-------------------------------确定

0 个答案:

没有答案