Henrik P. Hessel
Henrik P. Hessel

Reputation: 36617

LINQ Insert - No Error but no insert either

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();
                    }

Upvotes: 0

Views: 347

Answers (2)

James
James

Reputation: 82096

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

Upvotes: 1

Jeff Yates
Jeff Yates

Reputation: 62367

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.

Upvotes: 1

Related Questions