none
Loading Grid from EF gives ObjectContext error RRS feed

  • Question

  • I'm trying to load a grid control with data from a stored procedure using entity framework and I'm getting the following error: " The ObjectContext instance has been disposed and can no longer be used for operations that require a connection".    I had been using the commented out code below, which works, but wanted to use Entity Framework to call the stored procedure.  I've tried the code with and without the databind() without any difference.  the code for one of the stored procedures.  I have several other calls to stored proc's, which are called to Entity Framework that work fine, but they return distinct data values.  Any help appreciated.

    Tom

     {                  // Working code commented out  below 
                        //if (ViewMode == "Department")
                        //{
                        //    this.grdDutyCrew.DataSource = clsDept.GetAvailabilityDutyCrewByViewMode(clsDept.DeptID, StationID, Convert.ToDateTime(SelectedDate));
                        //}
                        //else
                        //{
                        //    this.grdDutyCrew.DataSource = clsDept.GetDutyCrewByDate(StationID, Convert.ToDateTime(SelectedDate));
                        //}
    // Entity Framework Call:
                        using (var context = new sfhsEntities())
                        {
                            if (context.DatabaseExists())
                            {
                                if (ViewMode == "Department")
                                {
                                    this.grdDutyCrew.DataSource = context.GetAvailabilityDutyCrewByViewMode(clsDept.DeptID, StationID, SelectedDate);
                                }
                                else
                                {
                                    this.grdDutyCrew.DataSource = context.GetDutyCrewByDate(StationID, SelectedDate);
                                }
                            }
                            this.grdDutyCrew.DataBind();
                        }
                    }
    //One of the Called Stored Procedure:
      public DataSet GetDutyCrewByDate(int StationID, DateTime SelectedDate)
        {
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            if (Conn.ObjConn.State == ConnectionState.Connecting)
            {
                return null;
            }

            if (Conn.ObjConn.State != ConnectionState.Open)
            {
                Conn.GetConnection();
            }
            SqlCommand cmd = Conn.ObjConn.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "GetDutyCrewByDate";
            cmd.Parameters.Add("@rc", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
            cmd.Parameters.Add("@StationID", SqlDbType.Int).Value = StationID;
            cmd.Parameters.Add("@SelectedDate", SqlDbType.DateTime).Value = SelectedDate;

            try
            {
                da.SelectCommand = cmd;
                da.Fill(ds);
                 return ds;
            }
            catch (Exception ex)
            {
                ErrMsg = ex.Message;
                return null;
            }
            finally
            {
                if (Conn.ObjConn != null)
                {
                    ((IDisposable)Conn.ObjConn).Dispose();
                }
            }
        }
    • Edited by Tom Loach Wednesday, November 26, 2014 5:10 PM
    Wednesday, November 26, 2014 5:09 PM

All replies

  • What shocks me on this is that you are using EF, and then you start using dataset and datatable. What's the purpose of using an ORM, which is about using objects and not datatable and dataset. What's wrong with using a List<T> of custom objects and bind that to the control? The List <T> is a disconnected solution and send back the List<T>.
    Wednesday, November 26, 2014 11:11 PM
  • Hello Tom,

    Being confused why you want to call store produce with this way. Since you already use the Entity Framework, you could import the store procedures to the project and call it directly, here is an example shows these operations:

    http://www.entityframeworktutorial.net/stored-procedure-in-entity-framework.aspx

    And for this error “The ObjectContext instance has been disposed and can no longer be used for operations that require a connection", usually, it is because the context object is disposed before binding the data. In your case, please define a page dbcontext instance without the “using” or using the List<T> as darnold mentions.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 27, 2014 1:58 AM
    Moderator
  • Thanks very much for the help.  The reason for using EF was to learn how to use it and I chose to use part of an application I already had a working grid. I'll try your suggestion.

    Thanks,
    Tom

    Friday, November 28, 2014 7:48 PM