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();
}
}
}
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();
}
}
}