我已经搜索了这个问题的答案,但似乎无法找到适合我的情况,或者我还没有掌握它。我正在为我公司的商业软件制定业务规则,并且收到“指定的演员表无效”错误。我知道它与char变量和/或null有关,我只是无法弄清楚如何修复它或为什么我得到错误(它可能很简单)。
以下是规则的代码。它应该将unit_price与price1(表中的两个字段)进行比较,如果price1更高,则将unit_price替换为price1,并且对表中的每一行执行此操作。
我有代码工作,直到我尝试添加Class4。 Class4指定具有锁定价格的项目。这用于防止价格锁定的商品价格变动。 Class4是表中的char [8]字段,有两个值,LOCKED和null。这是我的问题开始的地方。
为了合法目的,我删除了对该计划的引用。
如果我的代码混乱或完全无效或不正确,请原谅我。我对c#很新,并且大部分都在猜测。
public class PriceCheck : program.Extensions.BusinessRule.Rule
{
decimal Unit = 0m;
decimal Price1 = 0m;
String Class4;
DataTable datatable1;
Boolean bDebugMode = true;
int TriggerRow;
public override RuleResult Execute()
{
RuleResult result = new RuleResult();
result.Success = true;
if (bDebugMode) MessageBox.Show("Starting PriceCheck"); //MessageBox for debugging purposes, shows that PriceCheck has begun
try
{
if (this.Session.MultiRow)
{
datatable1 = this.Data.Set.Tables["Table"]; //Set table information will be pulled from and sent to
TriggerRow = this.Data.TriggerRow;
foreach (DataRow row in (InternalDataCollectionBase)datatable1.Rows) //Begin loop for all rows in datatable1
{
Unit = DataRowExtensions.Field<Decimal>(row, "unit_price"); //Retrieve Unit Price from current row
Price1 = DataRowExtensions.Field<Decimal>(row, "inv_mast_price1"); //Retrieve Price1 from current row
Class4 = Convert.ToString(DataRowExtensions.Field<Char?>(row, "class_id4")); //Retrives Class4 from current row
if (bDebugMode) MessageBox.Show("Unit Price " + Unit + "\n" + "Price 1 " + Price1 + "\n" + "Class 4" + Class4, "Debug", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); //MessageBox for debugging, shows values in Unit, List, and Class4
if (Class4 == "LOCKED")
{
if (bDebugMode) MessageBox.Show("Class 4 Locked" + "\n" + "Unit Price will remain the same!" + "\n" + "Unit Price " + Unit, "Class 4 Locked", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
if (Unit > 0) //if unit is null(0), the rule will fail, this is here to prevent that...might change to item Id, depending on if any items have 0 Unit Price and Price 1
{
if (bDebugMode) MessageBox.Show("Unit > 0", "Unit Check", MessageBoxButtons.OK, MessageBoxIcon.Information); //MessageBox for debugging, shows that this if statement has been reached
if (Unit < Price1) //beginning of calculations
{
if (bDebugMode) MessageBox.Show("Price 1 " + Price1, "Debug", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation); //Shows Price1 before putting it into the unit_price field
DataRowExtensions.SetField<Decimal>(row, "unit_price", Price1); //assigns Price1 to the unit_price field
}
else
{
if (bDebugMode) MessageBox.Show("Unit Price" + Unit, "Debug", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation); //Shows Unit Price before assigning it to the unit_price field
DataRowExtensions.SetField<string>(row, "unit_price", Convert.ToString(Unit)); //assigns Unit to the unit_price field
}
}
}
}
}
}
catch (Exception ex)
{
result.Success = false;
result.Message = ex.Message;
}
if (bDebugMode) MessageBox.Show("Ending Price Check"); //MessageBox for debugging, shows the end of the calculations before result is returned
return result;
}
public override string GetDescription()
{
return "Checks prices to keep Unit Price above minimum.";
}
public override string GetName()
{
return "Price Check";
}
}
}