It seems to be a severe and suspicious issue i have a simple for-each loop which iterates through a data table rows. My code was working fine for almost a year suddenly this issue appears a month ago.
I done text file based logging and observed that for-each loop skip some rows with-out any exception with in the code. I have a log in the very first line when loop begins which also not working for skipped items.
below is my code:
foreach (DataRow item in tblItemsToIssue.Rows)
{
Logger.LogInfo(string.Format("Start processing item = {0}", item["Item_Id"].ToString()));
Dept_Item_BatchWise dibw = new Dept_Item_BatchWise();
KeyValueCollection kvd = new KeyValueCollection();
kvd.Add("Item_Id", Convert.ToInt32(item["Item_Id"]));
kvd.Add("Department_Id", Convert.ToInt32(SessionObject.Get("User_Department_Id").ToString()));
DataTable dt = dibw.GetFiltered_Table(kvd, true, "Expiry_Date", true);
if (dt.Rows.Count > 0)
{
Logger.LogInfo("Batches found");
//Check if available batches have enough quantity to issue
int BatchesQuantitySum = 0;
foreach (DataRow bItem in dt.Rows)
{
BatchesQuantitySum += Convert.ToInt32(bItem["Quantity"]);
}
int QuantityNeedToIssue = Convert.ToInt32(item["Issue_Quantity"]);
if (BatchesQuantitySum >= QuantityNeedToIssue)
{
string documentDetailId = Convert.ToString(itemsDetailPharmacyIssuanceIds.GetByIndex(itemsDetailPharmacyIssuanceIds.IndexOfKey(item["Item_Id"])));
int QuantityInBatch = Convert.ToInt32(dt.Rows[0]["Quantity"]);
if (QuantityNeedToIssue > QuantityInBatch)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (QuantityNeedToIssue > 0)
{
int batchQty = Convert.ToInt32(dt.Rows[i]["Quantity"]);
if (batchQty <= 0) //skip current batch coz it has 0 quantity
continue;
int iQty = 0;
if (QuantityNeedToIssue <= batchQty)
{
iQty = QuantityNeedToIssue;
QuantityNeedToIssue -= iQty;
}
else
{
iQty = batchQty;
QuantityNeedToIssue -= iQty;
}
//Item Transaction Made
Dept_Item_Transaction dti = new Dept_Item_Transaction();
KeyValueCollection kv = new KeyValueCollection();
kv.Add("Document_Id", documentId);
kv.Add("Document_No", documentNo);
kv.Add("Document_Date", documentDate);
kv.Add("Document_Detail_Id", documentDetailId);
kv.Add("Item_Id", item["Item_Id"].ToString());
kv.Add("Unit_Id", item["Unit_Id"].ToString());
kv.Add("In_Out", "O");
kv.Add("Active", "Y");
kv.Add("Department_Id", Convert.ToInt32(SessionObject.Get("User_Department_Id").ToString()));
kv.Add("Unit_Price", item["Unit_Price"].ToString());
kv.Add("Employee_Id", SessionObject.Get("User_Employee_Id"));
kv.Add("Quantity", iQty);
if (DBNull.Value != dt.Rows[i]["Expiry_Date"] && !string.IsNullOrEmpty(dt.Rows[i]["Expiry_Date"].ToString()))
{
DateTime t;
DateTime.TryParse(dt.Rows[i]["Expiry_Date"].ToString(), out t);
if (t != DateTime.MinValue)
kv.Add("Expiry_Date", dt.Rows[i]["Expiry_Date"].ToString());
}
if (DBNull.Value != dt.Rows[i]["Batch_No"])
kv.Add("Batch_No", dt.Rows[i]["Batch_No"].ToString());
dti.PerformTransaction(DocumentType.Issuance, kv);
Logger.LogInfo(string.Format("Issuance from Batch: {0}", dt.Rows[i]["Batch_No"].ToString()));
}
else
{
Logger.LogInfo("Multi batch issuance done.");
return;
}
}
}
else
{
//Item Transaction Made
Dept_Item_Transaction dti = new Dept_Item_Transaction();
KeyValueCollection kv = new KeyValueCollection();
kv.Add("Document_Id", documentId);
kv.Add("Document_No", documentNo);
kv.Add("Document_Date", documentDate);
kv.Add("Document_Detail_Id", documentDetailId);
kv.Add("Item_Id", item["Item_Id"].ToString());
kv.Add("Unit_Id", item["Unit_Id"].ToString());
kv.Add("In_Out", "O");
kv.Add("Active", "Y");
kv.Add("Department_Id", Convert.ToInt32(SessionObject.Get("User_Department_Id").ToString()));
kv.Add("Unit_Price", item["Unit_Price"].ToString());
kv.Add("Employee_Id", SessionObject.Get("User_Employee_Id"));
if (DBNull.Value != dt.Rows[0]["Expiry_Date"] && !string.IsNullOrEmpty(dt.Rows[0]["Expiry_Date"].ToString()))
{
DateTime t;
DateTime.TryParse(dt.Rows[0]["Expiry_Date"].ToString(), out t);
if (t != DateTime.MinValue)
kv.Add("Expiry_Date", dt.Rows[0]["Expiry_Date"].ToString());
}
if (DBNull.Value != dt.Rows[0]["Batch_No"])
kv.Add("Batch_No", dt.Rows[0]["Batch_No"].ToString());
kv.Add("Quantity", QuantityNeedToIssue);
dti.PerformTransaction(DocumentType.Issuance, kv);
Logger.LogInfo("Single Batch Issuance");
}
}
else
{
Logger.LogInfo("Not available enoungh in stock : Process Terminate");
string error = string.Format("Internal error: Item Id '{0}' has not available enough in stock to issue.", item["Item_Id"].ToString());
throw new Exception(error);
}
}
else
{
int QuantityNeedToIssue = Convert.ToInt32(item["Issue_Quantity"]);
if (QuantityNeedToIssue > 0)
{
Logger.LogInfo("No batches found : Process Terminate");
string error = string.Format("Internal error: Item Id '{0}' is missing from table Dept_Item_Batchwise.", item["Item_Id"].ToString());
throw new Exception(error);
}
else
{
Logger.LogInfo("Item Skip: Issue quantity is 0");
}
}
}
LOG:
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Before save document total items count = 20
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
After save document total items count = 20
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Before save transaction total items count = 20
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 356
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 397
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 1281
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 579
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 3195
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 1886
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 1845
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 1080
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 3385
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 2702
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 1453
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 1448
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Issuance from Batch: 144
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Multi batch issuance done.
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
After save transaction total items count = 20
===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Transaction committed
Possible solution:
Copy
tblItemsToIssue
to a local (in-memory) table just before theforeach
.