This is a migrated thread and some comments may be shown as answers.

Load Grid from stored proc using entity framework

1 Answer 174 Views
Grid
This is a migrated thread and some comments may be shown as answers.
tcl4p
Top achievements
Rank 1
tcl4p asked on 25 Nov 2014, 08:32 PM
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();
            }
        }
    }

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 28 Nov 2014, 08:07 AM
Hello Tom,

This is not related to RadGrid in any way and is a general Entity Framework related issue, so I could suggest that you take a look at the following forum threads, where some details on the matter could be found:
Hope this helps.


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
tcl4p
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or