Hi all,
Having 1 winForm with 2 grids bound to bindingsource controls: 1 grid for categories (master) and 1 grid for Products (detail) , data and relationship are well loaded.
Now, I try to add a new (master) record to categories:
For the next two cases I get issues:
First case: When I click "+" button without entering anything " New Category" is not added and I gat the message: "Cannot insert the value NULL into column 'CategoryName', table 'Northwind.dbo.Categories'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated." spite of filling it by code.
Second case: When I click "+" button and enter manually something in the CategoryName cell, after clicking save button it is persisted, but I get the message: "The changes to the database have been validated, but an error has occurred while updating the object context. ObjectContext might be in an inconsistent state. Internal exception message: AcceptChanges can not continue because the key values of the object are in conflict with another object in ObjectStateManager. Make sure key values are unique before calling AcceptChanges".
Here is my code:
namespace MyNameSpace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
NorthwindEntities ctx;
bool _adding;
private void Form2_Load(object sender, EventArgs e)
{
ctx = new NorthwindEntities();
categoriesBindingSource.DataSource = ctx.Categories;
}
private void categoriesBindingSource_AddingNew(object sender, AddingNewEventArgs e)
{
_adding = true;
}
private void categoriesBindingSource_CurrentChanged(object sender, EventArgs e)
{
if (_adding)
{
categoriesBindingSource.EndEdit();
var newCat = (Categories)categoriesBindingSource.Current;
if (newCat.Products == null)
{
Products Prd = new Products();
Prd.Discontinued = false;
Prd.ProductName = "New Product Name";
ctx.AddToProducts(Prd);
}
newCat.CategoryName = " New Category";
ctx.AddToCategories(newCat);
_adding = false;
}
}
private void categoriesBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
categoriesBindingSource.EndEdit();
ctx.SaveChanges();
}
Please any help ?
Loading
SamioPosted Jun 21, 2012, 6:24 AM
tried your solution and the issue remains the same.
Thank you
brunda kPosted Jun 21, 2012, 3:01 AM
{
categoriesBindingSource.EndEdit();
productsBindingSource.EndEdit();
ctx.SaveChanges();
}
http://blogs.msdn.com/b/bethmassi/archive/2009/09/15/inserting-master-detail-data-into-a-sql-server-compact-edition-database.aspx
SamioPosted Jun 20, 2012, 9:37 AM
1/- Adding or removing this:
categoriesBindingSource.EndEdit()
to categoriesBindingSource_CurrentChanged event do nothing while running the code.
2/- I assume that you proposed to persist first the parent to have an ID, so I used it like this:
newCat.CategoryName = " New Category";
ctx.AddToCategories(newCat);
if (newCat.Products == null)
{
Products Prd = ctx.Products.Where(c=>c.ProductID == newCat.CategoryID).First();
Prd.Discontinued = false;
Prd.ProductName = "New Product Name";
ctx.AddToProducts(Prd);
}
But finally the the issues remain the same.
Just for information, I've tried the same task using Linq 2 SQL instead of EF, it run like a charm.
Posted Jun 19, 2012, 8:16 PM
1) You've called " categoriesBindingSource.EndEdit(); " . Is this correct ?
2) First add the category into context object and add the product into context object.
newCat.CategoryName = " New Category";
ctx.AddToCategories(newCat);
if (newCat.Products == null)
{
Products Prd = new Products();
Prd.Discontinued = false;
Prd.ProductName = "New Product Name";
ctx.AddToProducts(Prd);
}
First make sure, the parent object is there or not. If it is not there create the parent object and refer the parent id to the child object.
If you want to do any validation before adding category, please do it.
Let me know, if this is helps you or not.