Getting the error in the system monitor System.InvalidCastException: Object cannot be cast from DBNull to other types.
I have gone through the code and added null checks on everything I can see but am still getting the error.
We have not been able to get debugging to work on functions or BPM’s so I cannot step through the code to see what is causing the error.
outstrMsg = "";
var context = (Erp.ErpContext)Ice.Services.ContextFactory.CreateContext();
var erpContext = new Erp.Internal.Lib.CCredChk(context);
decimal curavail=0;
decimal regavail=0;
decimal avail=0;
string regplant="";
string regwhse="";
string alplant="";
string alwhse="";
//using(var txScope = Ice.IceContext.CreateDefaultTransactionScope())
//{
//foreach (var vDbOrderrelExist in erpContext.Db.OrderRel.Where(x => x.Company == "MJP" && x.OpenRelease==true && x.OrderNum==901896))
foreach (var vDbOrderrelExist in erpContext.Db.OrderRel.Where(x => x.Company == "MJP" && x.OpenRelease==true && x.BuyToOrder==false && (x.Plant=="MJP02" || x.Plant=="MJP10" || x.Plant=="MJP20" || x.Plant=="MJP30" || x.Plant=="MJP55")))
{
if(vDbOrderrelExist!=null)
{
//outstrMsg += "Order Number: " + vDbOrderrelExist.OrderNum.ToString() + " " + DateTime.Now + Environment.NewLine;
using (var svc = Ice.Assemblies.ServiceRenderer.GetService<Ice.Contracts.DynamicQuerySvcContract>(context))
{
Ice.Tablesets.DynamicQueryTableset dsQuery = svc.GetByID("CheckAftermarketInventory");
if (dsQuery != null)
{
Ice.Tablesets.QueryExecutionTableset dsBAQ = svc.GetQueryExecutionParameters(dsQuery);
dsBAQ.ExecutionParameter[0].ParameterID = "pOrderNum";
dsBAQ.ExecutionParameter[0].IsEmpty = false;
dsBAQ.ExecutionParameter[0].ParameterValue = vDbOrderrelExist.OrderNum.ToString();
dsBAQ.ExecutionParameter[1].ParameterID = "pOrderLine";
dsBAQ.ExecutionParameter[1].IsEmpty = false;
dsBAQ.ExecutionParameter[1].ParameterValue = vDbOrderrelExist.OrderLine.ToString();
dsBAQ.ExecutionParameter[2].ParameterID = "pOrderRel";
dsBAQ.ExecutionParameter[2].IsEmpty = false;
dsBAQ.ExecutionParameter[2].ParameterValue = vDbOrderrelExist.OrderRelNum.ToString();
DataSet results = svc.Execute(dsQuery,dsBAQ);
if(results !=null)
{
//outstrMsg += "Allocation found " + vDbOrderrelExist.OrderNum.ToString() + "-" + vDbOrderrelExist.OrderLine.ToString() + "-" + vDbOrderrelExist.OrderRelNum.ToString() + " " + DateTime.Now + Environment.NewLine;
foreach (DataTable table in results.Tables)
{
if(table!=null)
{
if(table.TableName.Contains("Results"))
{
if(table.Rows.Count>0)
{
foreach (DataRow row in table.Rows)
{
if(row !=null)
{
if(row["Calculated_CurAvail"]!=null)
{
curavail=Convert.ToDecimal(row["Calculated_CurAvail"]);
}
if(row["Calculated_RegAvail"]!=null)
{
regavail=Convert.ToDecimal(row["Calculated_RegAvail"]);
}
if(row["Calculated_Avail"]!=null)
{
avail=Convert.ToDecimal(row["Calculated_Avail"]);
}
if(row["OrderHed_ShortChar02"]!=null)
{
regplant=row["OrderHed_ShortChar02"].ToString();
}
if(row["OrderHed_ShortChar02"]!=null)
{
regwhse=row["PartWhse2_WarehouseCode"].ToString();
}
if(row["PartPlant_Plant"]!=null)
{
alplant=row["PartPlant_Plant"].ToString();
}
if(row["PartPlant_PrimWhse"]!=null)
{
alwhse=row["PartPlant_PrimWhse"].ToString();
}
if(curavail>=(vDbOrderrelExist.OurReqQty - vDbOrderrelExist.OurStockShippedQty))
{
//outstrMsg += vDbOrderrelExist.OrderNum.ToString() + "-" + vDbOrderrelExist.OrderLine.ToString() + "-" + vDbOrderrelExist.OrderRelNum.ToString() + " Remaining in " + vDbOrderrelExist.Plant + Environment.NewLine;
}
if(curavail<(vDbOrderrelExist.OurReqQty - vDbOrderrelExist.OurStockShippedQty) && regavail>=(vDbOrderrelExist.OurReqQty - vDbOrderrelExist.OurStockShippedQty) && (regplant=="MJP02" || regplant=="MJP10" || regplant=="MJP20" || regplant=="MJP30" || regplant=="MJP55"))
{
this.CallService<Erp.Contracts.SalesOrderSvcContract>(osc => {
Erp.Tablesets.SalesOrderTableset sods = osc.GetByID(vDbOrderrelExist.OrderNum);
var desiredRelease = sods.OrderRel.Where(r => r.OrderLine == vDbOrderrelExist.OrderLine && r.OrderRelNum == vDbOrderrelExist.OrderRelNum);
foreach(var rel in desiredRelease)
{
rel.Plant=regplant;
rel.WarehouseCode=regwhse;
rel.PurPoint="";
rel.RowMod="U";
}
osc.ChangePlant(regplant,ref sods);
osc.Update(ref sods);
outstrMsg += vDbOrderrelExist.OrderNum.ToString() + "-" + vDbOrderrelExist.OrderLine.ToString() + "-" + vDbOrderrelExist.OrderRelNum.ToString() + " Allocated to " + regplant + DateTime.Now + Environment.NewLine;
});
}
if(curavail<(vDbOrderrelExist.OurReqQty - vDbOrderrelExist.OurStockShippedQty) && regavail<(vDbOrderrelExist.OurReqQty - vDbOrderrelExist.OurStockShippedQty) && avail>=(vDbOrderrelExist.OurReqQty - vDbOrderrelExist.OurStockShippedQty))
{
this.CallService<Erp.Contracts.SalesOrderSvcContract>(osc => {
Erp.Tablesets.SalesOrderTableset sods = osc.GetByID(vDbOrderrelExist.OrderNum);
var desiredRelease = sods.OrderRel.Where(r => r.OrderLine == vDbOrderrelExist.OrderLine && r.OrderRelNum == vDbOrderrelExist.OrderRelNum);
foreach(var rel in desiredRelease)
{
rel.Plant=alplant;
rel.WarehouseCode=alwhse;
rel.PurPoint="";
rel.RowMod="U";
}
osc.ChangePlant(alplant,ref sods);
osc.Update(ref sods);
outstrMsg += vDbOrderrelExist.OrderNum.ToString() + "-" + vDbOrderrelExist.OrderLine.ToString() + "-" + vDbOrderrelExist.OrderRelNum.ToString() + " Allocated to " + alplant + DateTime.Now + Environment.NewLine;
});
}
}//Match allocation to order line
}
}// loop through allocation
}
}
}
}//Loop through order lines
}//check if data in allocation table
}//build allocation table
}
}//loop order releases that are open in specific plants
// txScope.Complete();
//}
if(outstrMsg=="")
{
outstrMsg+="No Order Releases to Allocate " + Environment.NewLine;
}
System.IO.File.AppendAllText(@"\\appservername\Supporting Files\Logs\AftermarketAllocation.txt", outstrMsg.Trim() + " " + DateTime.Now + " " + Session.AppServerURL + Environment.NewLine);```