I have created an API to add multiple items. I use transaction scope because I don't have to insert all items if found a problem in any item. so I have used TransactionScope so doing dispose of the transaction if found any validation failed in an item. I use a loop of 100 items to avoid database time out, but in the loop, I got error like
"The requested operation cannot be completed because the connection has been broken".
please let me know any solution tom fulfill my requirements
I send XML to the database of each 100 items in a loop. code is a per below
using (TransactionScope transactionScope = new TransactionScope())
{
try
{
for (int i = 0; i < objPricebook.ItembookMasterObject.Length; i = i + 100)
{
List<ItembookMasterObject> items = objPricebook.ItembookMasterObject.Skip(i).Take(100).ToList();
string ItemXML = CreateXML(items); // CreateXML is function i have created to convert data in to XML
if (ItemXML != "")
{
DS = obj.AddItemBook(ItemXML, objGuid.ToString());
if (DS.Tables.Count > 0)
{
if (DS.Tables[0].Rows.Count > 0)// this means any validation is failed.
{
transactionScope.Dispose();
var id = obj.AddPriceBookLogError(DS.Tables[0]);
return Json(new { status = "Failed", message = "There is some problem with Item json });
}
}
}
}
}
catch (Exception ex)
{
transactionScope.Dispose();
return Json(new { message = "Item Json has some invalid imput", exceptionMessage = ex.Message, errorCode = "009" });
}
transactionScope.Complete();
transactionScope.Dispose();
return Json(new { status = "Success", message = "Price book is successfully added" });
}