Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9070477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:41:46+00:00 2026-06-16T17:41:46+00:00

Good Afternoon, I’m currently attempting to work with the Entity Framework DbContext/Code First inside

  • 0

Good Afternoon,

I’m currently attempting to work with the Entity Framework DbContext/Code First inside a winforms application. My class, PrintQueueItem, is mapped to a view inside the database that only returns PrintQueueItem that have a PrintStatus equal to ‘Pending’. This is displayed to the end-user via a DataGridView so they can view what’s to be batch printed at the end of the day. Users are able to add instances of PrintQueueItem and this is displayed inside the DataGridView as well.

My issue arises when I attempt to cancel an item that’s set to print. I set the PrintStatus to Cancelled and then run the ExecuteSqlCommand to update the database. After this, I try reload the information but the item is still displayed inside the DataGridView. I’m unsure why this is as when I get the count (via Local) after the information has been loaded again it has been decremented by one. Any idea why this would be as I thought the BindingList provides a 2-way sync for data-binding and the UI would be updated with the change in data?

PrintQueueItem (Model)

public class PrintQueueItem
{
    public PrintQueueItem()
    {
        this.PrintQueueID = Guid.NewGuid();
        this.PrintStatus = "Pending";
        this.DateAdded = DateTime.Now;
    }

    public Guid PrintQueueID { get; set; }
    public Guid DocumentID { get; set; }
    public string PrintStatus { get; set; }
    public DateTime DateAdded { get; set; }

    public virtual Documentation Document { get; set; }
}

Initial Binding

this.printQueueBinding.DataSource = db.PrintQueue.Local.ToBindingList();
this.printQueueGridView.AutoGenerateColumns = false;
this.printQueueGridView.DataSource = printQueueBinding;

Update PrintStatus on PrintQueueItem and Reload

PrintQueueItem item = printQueueBinding.Current as PrintQueueItem;
if (item == null)
{
    throw new ArgumentNullException("Could not obtain instance of selected 'PrintQueueItem'");
}
item.PrintStatus = "Cancelled";
this.db.Database.ExecuteSqlCommand("exec usp_PrintQueue_Update {0}, {1}",item.PrintQueueID, item.PrintStatus);
this.db.PrintQueue.Load();

Some Additional Notes

Instead of a call to the ExecuteSqlCommand, I tried calling SaveChanges but encountered an exception stating:

"Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded." 

I’m assuming this is because the item is no longer found in the view when it’s updated. Please let me know if this is not the case.

The reason for using a filtered view rather than filtering in the application is because BindingList doesn’t implement IBindingListView and I’m unable to implement the BindingList.Filter method to filter the data displayed inside the DataGridView. Any suggestions on this are welcome as well.

Thank you for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:41:47+00:00Added an answer on June 16, 2026 at 5:41 pm

    After a couple beers and some time to let the jumbled mess of logic I had in my brain clear out from earlier today; I began to think of what was actually happening and then it hit me that when I attempted to call SaveChanges a concurrency exception was happening.

    I failed to pay attention to the type of exception that was raised and thus failed to properly handle it. I’ve since updated my code to the following so when a DbUpdateConcurrencyException occurs (expected, since the view no longer contains the record) the entities that experience the issue are refreshed from the database:

            try
            {
                if (Program.YesNoQuestion(string.Format("Cancel print job for the selected document?", printQueueGridView.SelectedRows.Count)) != System.Windows.Forms.DialogResult.Yes)
                {
                    return;
                }
    
                PrintQueueItem item = printQueueBinding.Current as PrintQueueItem;
                if (item == null)
                {
                    throw new ArgumentNullException("Could not obtain an instance of 'PrintQueueItem'");
                }
    
                item.PrintStatus = "Cancelled";
                this.db.Database.ExecuteSqlCommand("exec usp_PrintQueue_Update {0}, {1}", item.PrintQueueID,
                                                                                               item.PrintStatus);
                //this.db.PrintQueue.Load();
                this.db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                foreach (DbEntityEntry entry in ex.Entries)
                {
                    entry.Reload();
                }
            }
    

    Please let me know if there are better ways to go about this as I’m always open to other ideas and always interested in doing things the proper way. Have a great night!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good Afternoon, I'm currently trying to build something incredibly simple inside of the Google
Good afternoon, I have a listview filled using linqdatasource + entity framework iqueryable query.
Good afternoon, I am currently in the very early phase of a new project
Good afternoon, I'm having a problem with a section of code I'm working on
good afternoon,I'm trying to install mysql-5.5.29 source code on ubuntu. but cmake just shows:Configuring
Good afternoon, i am currently wrestling with an old .dll which functionality I have
Good Afternoon, I'm currently planning a web-app/service project with a geolocation-enabled user model (lat/lng
Good afternoon! I have some doubts about following code. [1] What does the middle
Good afternoon, I have a C++ class Range which implements a operator < for
Good afternoon, I am using the code below to map a network drive in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.