这似乎是一个严重且可疑的问题,我有一个简单的for-each循环,它遍历数据表行。我的代码在一个月前出现这个问题突然工作了将近一年。
我完成了基于文本文件的日志记录,并观察到for-each循环在代码中跳过了一些没有任何异常的行。循环开始时我在第一行有一个日志,这也不适用于跳过的项目。
下面是我的代码:
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
答案 0 :(得分:0)
可能的解决方案:
将tblItemsToIssue
复制到foreach
之前的本地(内存中)表。