views:

328

answers:

3
protected IEnumerable GetPersonalsData()
{
   // List<Personals> personel;
   using (FirmaEntities firmactx = new FirmaEntities())
   {
      var personeldata = (from p in firmactx.Personals select new { p.ID, p.Name, p.SurName });
      return personeldata.AsEnumerable();
   }
}

I want to send GetPersonelData() into GridView DataSource. Like this:

gwPersonel.DataSource = GetPersonelData(); 
gwPersonel.DataBind();

It monitored to me on : gwPersonel.DataBind(); this error:

"The ObjectContext instance has been disposed and can no longer be used for operations that require a connection."

A: 

Once the function returns, I think the context has disappeared so the enumeration can't be used. Just convert it to a list (use .ToList()) instead, this can be added to a DataSource.

Larry Watanabe
Larry; if i used var personeldata = (from p in firmactx.Personals select new { p.ID, p.Name, p.SurName }); gwPersonel.DataSource = personeldata.AsEnumerable();Every thing is ok but i need to return value from this value from function how?
programmerist
see above - use .ToList(). This will basically execute the query then, when the data context is still available. You can always convert the list back to an enumeration after the function returns, if that is what you need.
Larry Watanabe
A: 

When you return from the method, the object context is gone but the query has not been executed yet (deferred execution). You will either need to keep the object context around or ensure that the query executes and the results are available in GetPersonalData method by calling ToList().

Mehmet Aras
A: 

The problem is that AsEnumerable does exactly that returns an IEnumerable. The query you have defined doesn't get executed at that point. Only when something that receives it attempts to enumerate it will the query actually be executed. In this case that something is outside of GetPersonalsData and the instance of FirmaEntities that the query depends on will have been disposed by that point. Hence the error.

Earlier thoughts here but not so relevant in this case I suspect

You might consider using ToList() instead of ToEnumerable() which is in most scenarios a better solution. Another alternative would be:-

protected IEnumerable GetPersonalsData()
{
    // List personel;
    using (FirmaEntities firmactx = new FirmaEntities())
    {
        foreach (var item in (from p in firmactx.Personals))
           select new { p.ID, p.Name, p.SurName });
        {
            yield return item;
        }
    }
}

This is similar to but not exactly the same as a call AsEnumerable. In this case each time the returned IEnumerable is enumerated (its GetEnumerator method is called) a fresh execution of the function occurs (when the IEnumerator.MoveNext method is called). This allows you to defer (or not end up actually) executing the query at all. The instance of firmactx won't be disposed until the enumeration is complete.

After thoughts

You are assigning to a grid view which you may want to page and/or filter sort. In that case you might be better off holding on to an instance of the context as a field in your UserControl. Using this instance of the context assign the personeldata query directly to the DataSource without using AsEnumerable.

AnthonyWJones
Sir Antony ; your codes throws me :Server Error in '/' Application.A field or property with the name 'Detail' was not found on the selected data source.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.Web.HttpException: A field or property with the name 'Detail' was not found on the selected data source.
programmerist
Source Error:Line 23: gwCompany.DataBind();Line 24: gwPersons.DataSource = GetPersonalsData();//GetPersonalsPrm(1);//GetPersonalsData();//Line 25: gwPersons.DataBind();Line 26: Line 27: }
programmerist
i rearranged my GridView. There was Detail field. So i rearrenged gridView BoundField. Everything is ok Thanks Antohny!!!!
programmerist