Asked by:
Windows form BindingSource add new button not working

Question
-
Hi,
I am using entity framework code first with BindingSource for my windows form application. here is my existing code, When i click add new and enter data and save , if i load application again my new records not there. But when i edit existing one and save it, those changes effected. Why add new record not work?
private void Supplier_Load(object sender, EventArgs e) { poC = new POPContext(); supplierBindingSource.DataSource = poC.Suppliers.ToList(); } private void supplierBindingNavigatorSaveItem_Click(object sender, EventArgs e) { poC.SaveChanges(); }
Tuesday, February 28, 2012 4:28 PM
All replies
-
Hi Galgodage,
Welcome to MSDN Forum.
Based on the code, I don't find where you add the entity into the context, could you please post more code here? If you want to add an entity into database, before calling SaveChanges() method, you have to add the new entity into the context.
BestRegards
Allen Li [MSFT]
MSDN Community Support | Feedback to us
Thursday, March 1, 2012 4:33 AM -
Hi Galgodage,
Have you solved the issue?
Best Regards
Allen Li [MSFT]
MSDN Community Support | Feedback to us
Monday, March 5, 2012 2:55 AM -
Hi,
I have done this way
private void accountCallBindingNavigatorSaveItem_Click(object sender, EventArgs e) { if (Status == "Add") { DAL.AccountCall accountcall = new DAL.AccountCall(); accountcall.AccountId =int.Parse(accountIdComboBox.SelectedValue.ToString()); accountcall.CallDate = System.DateTime.Now; PopCon.AccountCalls.Add(accountcall); Status = ""; } PopCon.SaveChanges(); accountCallBindingSource.DataSource = PopCon.AccountCalls.ToList(); } private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e) { Status = "Add"; }
There is error raise point of savechanges()
The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.
Friday, March 9, 2012 3:53 PM -
Hi,
you can find two databinding walkthroughs under the http://msdn.microsoft.com/en-us/library/gg197521(v=vs.103).aspx topic.
Please let me know if that doesn't help you.
thank you,
Julia
This posting is provided "AS IS" with no warranties, and confers no rights.
Friday, March 9, 2012 6:29 PM -
Hi,
Do not bind the dataSource using toList() but to the object set. Otherwise the changes made in the data grid can not be tracked by the context object state manager.
Here is a small example code showing how to connect the data context to a DataGridView using a DataSource:
// EF data context // MyContext _context; // Load data from database into context // private void Form1_Load(object sender, EventArgs e) { // Create a new context with connection to the database _context = new MyContext(); // Connect the "Customers" ObjectSet with the binding source customerBindingSource.DataSource = _context.Customers; // not this: .ToList() ! } // Save changes in context back to database // private void saveToolStripButton_Click(object sender, EventArgs e) { int added = _context.ObjectStateManager.GetObjectStateEntries(EntityState.Added).Count(); int modified = _context.ObjectStateManager.GetObjectStateEntries(EntityState.Modified).Count(); int deleted = _context.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted).Count(); String msg = String.Format("New entries {0} - modified entries {1} - deleted entries {2}", added, modified, deleted); MessageBox.Show(msg, "Tracked changes"); _context.SaveChanges(); // We might want to sync with the database here // _context.Refresh(System.Data.Objects.RefreshMode.StoreWins, _context.Customers); }
Hope that helps,
Stefan
- Edited by Stefan Heesch Sunday, March 11, 2012 6:35 AM
Saturday, March 10, 2012 10:58 PM -
I tried this way
commentBindingSource.DataSource = PopCon.Comments;
then this error raise
Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList().
lakmal
Monday, March 12, 2012 3:42 AM -
Hi Galgodage,
Welcome to MSDN Forum.
Please add a break point at the line of your query statement, then debug line by line, to make sure whether the query result contains the new record.
Best Regards
Allen Li [MSFT]
MSDN Community Support | Feedback to us
Tuesday, March 13, 2012 3:18 AM -
Hi Galgodage,
Have you solved the issue? If you have any problems, please feel free to let me know.
Best Regards
Allen Li [MSFT]
MSDN Community Support | Feedback to us
Monday, March 19, 2012 2:07 AM