Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Disposing issue when trying to load data from SQLite database

Disposing issue when trying to load data from SQLite database

Scheduled Pinned Locked Moved C#
helpwpfquestioncsharpdatabase
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Code4Ever
    wrote on last edited by
    #1

    I'm using Syncfusion in my WPF MVVM project. I want to add pagination to my datagrid. The following code can do this:

    private void dataPager_OnDemandLoading(object sender, Syncfusion.UI.Xaml.Controls.DataPager.OnDemandLoadingEventArgs args)
    {
    using (var _sqliteContext = new SQLiteContext())
    {
    dataPager.LoadDynamicItems(args.StartIndex, _sqliteContext.Equipments.Include(x => x.CostCenter).Skip(args.StartIndex).Take(args.PageSize));
    //resetting cache for all pages.
    (dataPager.PagedSource as PagedCollectionView).ResetCache();
    }
    }

    The problem is that when I run the code, the application falls into the break mode and says:

    Quote:

    System.ObjectDisposedException: 'Cannot access a disposed context instance. A common cause of this error is disposing of a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'SQLiteContext'.'

    How can I fix it?

    L 1 Reply Last reply
    0
    • C Code4Ever

      I'm using Syncfusion in my WPF MVVM project. I want to add pagination to my datagrid. The following code can do this:

      private void dataPager_OnDemandLoading(object sender, Syncfusion.UI.Xaml.Controls.DataPager.OnDemandLoadingEventArgs args)
      {
      using (var _sqliteContext = new SQLiteContext())
      {
      dataPager.LoadDynamicItems(args.StartIndex, _sqliteContext.Equipments.Include(x => x.CostCenter).Skip(args.StartIndex).Take(args.PageSize));
      //resetting cache for all pages.
      (dataPager.PagedSource as PagedCollectionView).ResetCache();
      }
      }

      The problem is that when I run the code, the application falls into the break mode and says:

      Quote:

      System.ObjectDisposedException: 'Cannot access a disposed context instance. A common cause of this error is disposing of a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'SQLiteContext'.'

      How can I fix it?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Code4Ever wrote:

      This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement.

      Try reworking without the using statement.

      C 1 Reply Last reply
      0
      • L Lost User

        Code4Ever wrote:

        This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement.

        Try reworking without the using statement.

        C Offline
        C Offline
        Code4Ever
        wrote on last edited by
        #3

        Thanks. Solved using the following code rework:

        private void dataPager_OnDemandLoading(object sender, Syncfusion.UI.Xaml.Controls.DataPager.OnDemandLoadingEventArgs args)
        {
        List data;
        using (var _sqliteContext = new SQLiteContext())
        {
        data = _sqliteContext.Equipments.Include(x => x.CostCenter).Skip(args.StartIndex).Take(args.PageSize).ToList();
        }
        dataPager.LoadDynamicItems(args.StartIndex, data);
        //resetting cache for all pages.
        (dataPager.PagedSource as PagedCollectionView).ResetCache();
        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups