1

I have an existing project where I want to use an ObjectDataSource for a large data set which pages the results for a DevExpress Grid.

Examples of the code are shown below.

I am having trouble using an Entity Framework context in static methods that are required for the ObjectDataSource.

My problem is that I get this exception:

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

Does anyone know how I can change the below to support passing an Entities instance from the Page to the static methods?

This is the example of the ObjectDataSource

        <dx:ASPxGridView ID="DefinedReportGrid" runat="server" EnableTheming="True" Theme="Office2010Blue" EnableViewState="False" 
            ClientInstanceName="DefinedReportGrid" DataSourceForceStandardPaging="True" DataSourceID="ReportDataSource">
            <SettingsPager PageSize="30"></SettingsPager>
        </dx:ASPxGridView>

        <asp:ObjectDataSource ID="ReportDataSource" runat="server" EnablePaging="True" 
            StartRowIndexParameterName="startRecord" MaximumRowsParameterName="maxRecords" SelectCountMethod="GetPageCount" SelectMethod="GetData" 
    TypeName="ReportService">
        </asp:ObjectDataSource>

This is my service class with static methods that can then be used by the ObjectDataSource

 public class ReportService
    {
        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public static DataTable GetData(int startRecord, int maxRecords)
        {
        // Use EF DbContent and LINQ Query to fetch paged data, and return a DataTable
        // The data is PIVOTED in the code before returning it as a DataTable

            return outputTable;

        }

        public static List<int> GetWeeks()
        {
        // This also requires use of the EF DbContent
        ...
        }

        public static int GetPageCount()
        {
        // This also requires use of the EF DbContent
        // Used to count the full data for applying the correct total Page Count
        ...
        }
    }

This is the code behind for the web forms page

    public partial class DefinedReport : System.Web.UI.Page
    {
        private Entities _modelContext = ((Global)HttpContext.Current.ApplicationInstance).Entities;

        protected void Page_Init(object sender, EventArgs e)
        {
             ...
        }

        protected void Page_Load(object sender, EventArgs e)
        {
             ...
        }

    }

This is the global code, where the Entities context is being set on the begin and end of the request.

    public class Global : HttpApplication
    {
        public Entities Entities { get; set; }

    ...

        private void Application_BeginRequest(object sender, EventArgs e)
        {
            Entities = new Entities();
        }

        private void Application_EndRequest(object sender, EventArgs e)
        {
            if (Entities != null)
            {
                Entities.Dispose();
            }
        }
    }

2 Answers 2

1

As someone who uses the DevX grid quite a bit in a VB.Net Webforms app with static/Shared methods to encapsulate various data access logic, another approach would be to skip creating a ObjectDataSource in the ASPX and instead set up the grid's DataSourceID in the code-behind's Page_Load. I founded this much more manageable and predictable.

Here's the idea:

Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Using eFContext As New ApplicationEntities()
        Dim requestedTransactionId As Guid = GetAndCheckQueryStringValueGuid("TransactionId")
        Dim requestedTransaction As Transaction = Transaction.GetAndConfirm(eFContext, requestedTransactionId)

        Dim requestedCustomers As IQueryable(Of Object) =
            Customer.GetCustomersForCustomersGrid(eFContext, requestedTransaction)

        CustomersGridView.DataSource = requestedCustomers
        CustomersGridView.DataBind()
End Sub

I also create a new EF context for each request in Page_Load - I've not found the performance associated with this to be an issue and it makes hunting down problems 10x easier. You may want to consider this, at least to get started. I've seen others in the past suggest putting your context into Session state and fetching it from there when needed, but that feels like it could cause all sorts of hard-to-track-down badness so I've never been bold enough to try and make that work.

As a side note for if you follow my suggestion: if your initial load of the ASPxGridView works but you get errors as you move from page to page, sort columns, etc. then you may need to use some .Includes for associated entities that display in your grid.

6
  • Thanks for the suggestion, I think that might be the same as what I was doing before. I switched to using the ObjectDataSource because I was trying to find a way to make the DevEx Grid not request all my data every time I change a page item, because they don't seem to support setting the PageCount manually I was unable to do this myself, so started looking at what support DevEx have, this is the best example I found devexpress.com/Support/Center/Example/Details/E2672 but its only relevant if I was using a direct SQL connection, do you know of a way to approach this?
    – Pricey
    Commented May 1, 2014 at 20:47
  • 1
    @Pricey From your description, if I'm understanding you correctly it sounds like the first time the app needs the particular data you would create the necessary DataTable (using EF to obtain the data) and .DataBind the DataTable to the grid, saving it into Session (or somewhere else) on the way out of Page_Load to persist it between grid callbacks. Then, in subsequent requests, in Page_Load you would fetch the DataTable out of Session and .DataBind it to the grid so that it could use the data again without requerying the underlying database using EF.
    – JimMSDN
    Commented May 2, 2014 at 9:37
  • Yea I have done this in some of my other reports that are smaller, but in this case the data is around a million records and this application is already bloating the session too much when several users run the reports :-( so storing it in session to make subsequent paging loads faster in the grid wasn't really feasible.
    – Pricey
    Commented May 2, 2014 at 9:54
  • 1
    @Pricey Ugh - I almost added to my last comment "...unless the size of the data makes persisting those results infeasible"...8-) Next solutions might look like moving some of the data manipulation logic onto the database as SProcs and using EF to bind those results back into objects that ASPxGridView can then consume - something like weblogs.asp.net/zeeshanhirani/archive/2010/11/02/… or msdn.microsoft.com/en-us/library/vstudio/cc716672.aspx
    – JimMSDN
    Commented May 2, 2014 at 10:04
  • Thanks for the links, looks useful, unfortunately in this particular case I am having to pivot the data in the code prior to applying it to the grid, if I was in a position to change this particular SQL then I would use a SQL PIVOT during a load process and store it in a table because this data is refreshed daily, but in my case I can't at the moment. Thanks for the help, take a look at the answer I have added and see what you think.
    – Pricey
    Commented May 2, 2014 at 10:20
0

I have since found this example: http://www.asp.net/web-forms/tutorials/continuing-with-ef/using-the-entity-framework-and-the-objectdatasource-control,-part-1-getting-started

which made me realise that my methods on my ReportService class don't need to be static in order for this to work, which was my main problem in the first place.. I am just not experienced enough with ObjectDataSource and was following the examples that DevExpress gave from here: http://www.devexpress.com/Support/Center/Example/Details/E2672

So now a new EF ObjectContext can be created in the scope of the class and then Disposed of when that class is disposed and this seems to work for me.

Like shown below:

public class ReportService : IDisposable
{
    private bool disposedValue = false;
    private Entities _modelContext = new Entities();

    public DataTable GetData(int startRecord, int maxRecords)
    {
    ...
    // use _modelContext and process the data
        return outputTable;

    }

    public List<int> GetWeeks()
    {
    ...
    // use _modelContext and process the data
    }

    public int GetPageCount()
    {
    ...
    // use _modelContext and process the data
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposedValue)
        {
            if (disposing)
            {
                _modelContext.Dispose();
            }
        }
        this.disposedValue = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

}
2
  • For my own clarity: when you say "service" do you mean a separate service/daemon-type app which runs on its own apart from your web app, an ASP.NET Web Service/WCF service, or just the concept of a "servicing" class that encapsulates functionality to other parts of your web app?
    – JimMSDN
    Commented May 2, 2014 at 10:45
  • Just the concept of a class that is servicing this particular report, it was originally just in the page code behind but the ObjectDataContext expects a TypeName for the class it will use for its specific methods
    – Pricey
    Commented May 2, 2014 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.