tags:

views:

106

answers:

2

I'm using the following code to insert a new entry in my existing db. But the new record doesn't get inserted. What are the options to fetch a possible exception? What could be the reason that this doesn't work?

Thanks,
rAyt

using (ContactManagerSampleDataDataContext db = new ContactManagerSampleDataDataContext())
                    {
                        CustomerCompany company = new CustomerCompany();
                        company.CompanyName = "Test";
                        company.IsActive = true;
                        company.ModifiedDate = DateTime.UtcNow;
                        company.SapNumber = 1;
                        company.CompanyId = 1;

                        db.CustomerCompanies.InsertOnSubmit(company);
                        db.SubmitChanges();
                    }
+1  A: 

If SubmitChanges is asynchronous, then the db object could be disposed before it has chance to finish, however, I don't see any evidence that it is asynchronous. Perhaps wrapping the SubmitChanges call in a try/catch block will indicate any exceptions that have occurred. You could also look at the DataContext.ChangeConflicts to see if any conflicts occurred.

Jeff Yates
any way to prevent this from happening?
Henrik P. Hessel
I don't believe the call is asynchronous, but if it were, you would need to postpone the call to Dispose() until you were sure that the commit had finished.
Jeff Yates
You can test this easily by not wrapping the object with the using keyword, although this is not a fix - more just a way of determining where the problem lies.
Jeff Yates
Actually it was a missing primary key in my imported table. But no way you guys could know that :)
Henrik P. Hessel
+1  A: 

You could wrap the db.SubmitChanges() with a try catch and see if anything is thrown. It could also be a concurrency issue.

James