tags:

views:

81

answers:

2

I am using LINQ to SQL. Is the following code proper if I want to notify the sender that the database was updated successfully or is there a better way?

           try
           {
               dc.ModelA.InsertOnSubmit(modela);
               dc.SubmitChanges();
               return true;
           }

           catch
           {
               return false;
           }
+3  A: 

The better way is to not catch the exception and let it propagate to the caller. By catching the exception you are removing all information about why the insert failed, making it very hard for anyone to debug and fix the problem. So you just need this:

dc.ModelA.InsertOnSubmit(modela);
dc.SubmitChanges();
Mark Byers
A: 

Cleaner approach would be to wrap it in TransactionScope:

using (var scope = new TransactionScope())
{
   dc.ModelA.InsertOnSubmit(modela);
   dc.SubmitChanges();
   scope.Complete();
}
Shankar Ramachandran