I am using LINQ To SQL and calling stored procedures to insert the new rows.
- Does the code below demonstrate an appropriate set of steps?
- I want to insert (other fours) only if all four inserts are passed otherwise rollback all four inserts.
public bool InsertNewInquiry(Inquiry inq)
{
using (var transaction = new TransactionScope())
{
using (DataContextDataContext dc = conn.GetContext())
{
foreach (Officer po in inq.Recipients)
{
int results1 = (int)dc.**spOffice_Insert**(po.Id,po.Name).ReturnValue;
}
foreach (Lookups tags in inq.Tags)
{
int results2 = (int)dc.**spTags_Insert**(po.Id,po.Name).ReturnValue;
}
foreach (Phone phone in inq.Phone)
{
int results3 = (int)dc.**spPhone_Insert**(po.Id,po.Name).ReturnValue;
}
int results4 = (int)dc.spInquiry_Insert(
inq.Id
,inq.StatusId
,inq.PriorityId
,inq.Subject).ReturnValue;
if (results1 != 0 && results2 != 0 && results3 != 0 && results4 != 0)
{
transaction.Complete();
return true;
}
return false;
/* old code:
transaction.Complete();
if (results == 0)
return false;
else
return true; */
}
}
}
I have tried the above code, but it does not insert the foreach insert (results1...results3)
Any idea how I can make this work as desired?