Question

I'm using a Linq to Sql bindingsource on a winform tied to a datagridview. After I do an AddNew and SubmitChanges it does not seem to save to the database.

var myObject = (myObject)myBindingSource.AddNew();
myObject.ID=123;
myObject.prop="test";
myDataContext.SubmitChanges();

then when I check the database the object is not there. Doesn't the datacontext get told by the bindingsource that I want to insert an object?

If I change the code to:

var myObject = (myObject)myBindingSource.AddNew();
myObject.ID=123;
myObject.prop="test";
myDataContext.MyObjects.InsertOnSubmit(myObject);
myDataContext.SubmitChanges();

it works. It seems double work since I ws expecting the bindingsource to take care of it.

No correct solution

OTHER TIPS

This question is kind of old - I actually forget I had asked it myself! So it's weird to come across it in a search for the same issue more than a year later. I did find a solution though.

At first, I used something like this (which doesn't work):

myBindingSource.DataSource = myLinqToSqlDataContext.SomeTable.ToList();

When I used:

myBindingSOurce.DataSource = myLinqToSqlDataContext.SomeTable.Take(20);

the bindingsource will update the underlying database for inserts and deletes without problems.

So I quess it's important to make sure the datasource is an IQueryable instead of a List.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top