c# - Business Rule - Specified cast is not valid -
i have searched answer question, , can't seem find situation, or haven't grasped yet. working on business rule company's business software , receiving "specified cast not valid" error. know has char variable and/or null, can't figure out how fix or why i'm getting error (it's simple).
below code rule. supposed compare unit_price price1, both fields in table, , replace unit_price price1 if price1 higher, , every row in table.
i had code working until tried add class4. class4 specifies items locked prices. used prevent changing prices of items locked prices. class4 char[8] field in table, , has 2 values, locked , null. issues began.
i have removed references program legal purposes.
forgive me if code messy or inefficient or incorrect. i'm new c# , have been guessing part.
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 debugging purposes, shows pricecheck has begun try { if (this.session.multirow) { datatable1 = this.data.set.tables["table"]; //set table information pulled , sent triggerrow = this.data.triggerrow; foreach (datarow row in (internaldatacollectionbase)datatable1.rows) //begin loop rows in datatable1 { unit = datarowextensions.field<decimal>(row, "unit_price"); //retrieve unit price current row price1 = datarowextensions.field<decimal>(row, "inv_mast_price1"); //retrieve price1 current row class4 = convert.tostring(datarowextensions.field<char?>(row, "class_id4")); //retrives class4 current row if (bdebugmode) messagebox.show("unit price " + unit + "\n" + "price 1 " + price1 + "\n" + "class 4" + class4, "debug", messageboxbuttons.okcancel, messageboxicon.information); //messagebox debugging, shows values in unit, list, , class4 if (class4 == "locked") { if (bdebugmode) messagebox.show("class 4 locked" + "\n" + "unit price remain same!" + "\n" + "unit price " + unit, "class 4 locked", messageboxbuttons.ok, messageboxicon.exclamation); } else { if (unit > 0) //if unit null(0), rule fail, here prevent that...might change item id, depending on if items have 0 unit price , price 1 { if (bdebugmode) messagebox.show("unit > 0", "unit check", messageboxbuttons.ok, messageboxicon.information); //messagebox debugging, shows 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 unit_price field datarowextensions.setfield<decimal>(row, "unit_price", price1); //assigns price1 unit_price field } else { if (bdebugmode) messagebox.show("unit price" + unit, "debug", messageboxbuttons.okcancel, messageboxicon.exclamation); //shows unit price before assigning unit_price field datarowextensions.setfield<string>(row, "unit_price", convert.tostring(unit)); //assigns unit unit_price field } } } } } } catch (exception ex) { result.success = false; result.message = ex.message; } if (bdebugmode) messagebox.show("ending price check"); //messagebox debugging, shows end of calculations before result returned return result; } public override string getdescription() { return "checks prices keep unit price above minimum."; } public override string getname() { return "price check"; } } }
Comments
Post a Comment