Lightswitch: How to multiply two fields together? - c#

Am new to this guys need your help! Am trying to multiply two field and the answer to that will be the result of my computed field. One field is from another table..
partial void SubTotal_Compute(ref decimal result)
{
// Set result to the desired field value
result = this.Quantity * this.Rate.PulaPerUnit;
}
Every-time i try to add a new record to the table i get a Null Reference exception

Rate is likely to be null. You should add a test for it before performing your calculation, e.g.
if (Rate != null)
{
result = Quantity * Rate.PulaPerUnit;
}
You should probably ensure that Rate has been set before you call Subtotal_Compute.

Related

Why Datagrid in WPF can sort when all data in that column is the same?

I have a question. When I try to make a custom sorting data for my datagrid. I found a problem is: Sorting is work when all data on each cell of that column is the same.
More detail: I have a list include (id, name) id is unique, but all name = "* *". This symbol is japanese.
I had try to make a class inherit Icomparer and override Comparer() method with
CompareInfo compInfo = new CompareInfo.GetCompareInfo("ja-JP");
return compInfo.Compare(str1,str2, CompareOption.StringSort) * mode;
Mode is direction of sort
I had to try all of CompareOption. Include using Strings.StrComp() of VB but it still returns a different result when sort. My expect is when all of the value of that column is the same, index of item is my datagrid will not change.
Thanks, sorry my bad english!
The problem is that the sort order is indeterminate when the names are the same. Sort also by id when the names are equal.
CompareInfo compInfo = new CompareInfo.GetCompareInfo("ja-JP");
int result = compInfo.Compare(str1, str2, CompareOption.StringSort);
if (result == 0) { // str1 and str2 are equal.
result = compInfo.Compare(id1, id2);
}
return result * mode;
Assuming the id is a string. If the ids are int, then compare
if (result == 0) { // str1 and str2 are equal.
result = id1.CompareTo(id2);
}
Note: CompareInfo.Compare and Int32.CompareTo return -1 if the first value is smaller than the second, +1 the first is lager than the second and otherwise, when both values are equal, 0.

In Lightswitch, how do I make a calculated field/property in an Entity/Table based on data in a record from a separate table?

I currently have a formula in c# for a calculated property in a Lightswitch Entity/Table (HouseIncome).
result = NumberInHouseHold >= 1
? (AnnualHouseHoldIncome / (7880 + (NumberInHouseHold * 4180)))
: 0;
The NumberInhousehold has a data type of Decimal. The AnnualHouseHoldIncome is a data type of Money. The two numbers are variables which are changed each year. The PFPLevel is a calculated property with a Percent data type. For each record in this Table there is also a EmploymentDate property with a Date data type.
This calculation works great for 2017 but in 2018, and subsequent years, I would have to recode the calculated field with the new guidelines and information displayed in the PFPLevel field would no longer be accurate for 2017.
I would like to make the PFPLevel calculation based on data entered in a record of the HouseIncome table and the variables drawn from the FPLGuidlines table according to the Employment date. So, if a person in 2017 had a record with an employment date of 6/17/2017, that record would have the PFPlevel calculated on with NumberInHousehold, AnnualHouseHoldIncome, and Guidelines drawn from 2017 record.
In the FPLGuidelines table there would be four fields; Id, YearID, BaseIncome, PerIndividulalAmount which would have a new record each year. I am not sure what data types I would need for each of these properties/fields.
One option would be to use the following type of entity data types:
Alongside the following type of approach to your calculated property:
partial void PFPLevel_Compute(ref decimal result)
{
if (EmploymentDate.HasValue)
{
var hiy = EmploymentDate.Value.Date.Year;
var gl = DataWorkspace.ApplicationData.FPLGuidelines.Where(e => e.Year == hiy).Execute().FirstOrDefault();
if (gl != null)
{
result = NumberInHouseHold >= 1
? (AnnualHouseHoldIncome / (gl.BaseIncome + (NumberInHouseHold * gl.PerIndividualAmount)))
: 0;
}
}
}
The above example is based on your HouseIncome.EmploymentDate property being a nullable DateTime.

Doesn't update the value on the screen

I use this code for get the new value for the product, but i want to see this one on the screen, i used:
”row.UnitPrice”, but the data is there, but no on the screen.
So i need to call some another funtion for update it?
protected void SOLine_InventoryID_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e)
{
var row = (SOLine)e.Row;
row.UnitPrice = null;
if (row.OrderType == "SO")
{
if (row.InventoryID != null)
{
InventoryItem oItem = PXSelect<InventoryItem,Where<InventoryItem.inventoryID,Equal<Required<InventoryItem.inventoryID>>>>.Select(new PXGraph(), row.InventoryID);
if (oItem != null)
{
decimal? qty = row.Qty;
row.UnitPrice = CalcLinePrice( oItem.RecPrice,qty);
Base.Transactions.Update(row);
}
}
}
}
protected decimal? CalcLinePrice(decimal? unitPrice, decimal? qty)
{
return unitPrice *2 * (qty);
}
Agree with Hybridzz: you should update CuryUnitPrice field instead of UnitPrice. Also it's necessary to use PXCache.SetValueExt method to raise all field-level handlers for the CuryUnitPrice field when assigning new value:
sender.SetValueExt<SOLine.curyUnitPrice>(e.Row, price);
On a side note:
you should never invoke Update method for currently processed record in FieldUpdated handlers - Base.Transactions.Update(row); must go away
static PXSelectorAttribute.Select method must be used to retrieve InventoryItem selected for SOLine record:
the PXSelectorAttribute stores records in single GlobalCache storage shared among all user sessions: it's a more efficient option in comparison to QueryCache of the PXView class accessible only for 1 user
reduce maintenance costs as you don't need to update BQL queries in multiple places if someone change BQL query for the SOLine.InventoryItem field
The 'Unit Price' column in the Details grid is associated with the field CuryUnitPrice and not the UnitPrice.
So you might need to update the correct DAC field.
row.CuryUnitPrice = CalcLinePrice( oItem.RecPrice,qty);

c# int value not selected correctly with linq query

I've build a database using Entity Framework Code First.
I was trying to do a simple LINQ query ad display data of a table in a listbox, but an int value always results as 0.
So I tried to do a debug and in XAML.CS at this point:
var item = (from Item in db.Items
select Item ).ToList();
My "Price" value is 0 when the entity is selected, but it should be 7
EDIT
Item.cs
public int Price
{
get { return price; }
set { price = value / 100; }
}
set { price = value / 100; }
There's your problem... I don't know what that's supposed to do, but remove the / 100 and it'll work.
7 / 100 is 0, since you're doing integer division.
If you want to show prices with decimal fractions, make the property and column a decimal.
Alternatively, if you're stuck with the int column for some reason, see How to map column and entity propery of different datatypes in entity framework code first how to do that:
// An int property to map to your database:
[Column("Price")]
public int PriceInt { get; set; }
// Use this property from code.
[NotMapped]
public decimal Price
{
// Cast to decimal for decimal division.
get { return (decimal)PriceInt / 100; }
set { PriceInt = (int)(value * 100); }
}

How do you Request[""] with a Dynamic Variable? Request["#Variable"]?

I'm building a form, where the number of questions, or inputs on the form varies depending on the value in a database.Each input on the form is a radio type. The name of the tags are dynamic and are loaded from the database using #db.row.questionID which would look something like: <span name=#id> and equal a value of 1 through whatever queries were requested.
My issue is, i wrote the form using post, and i want to submit the values back into a separate database, but i dont know how to request multiple values, that changes dynamically based on query.
Sample code i wrote, it doesnt give me any errors, but it doesnt give me any results either.
foreach(var prow in poll){
var Question = prow.PollId;
if (Request.Form["#prow.PollId"] == "A") {
int AnsA = row.ResultsA;
AnsA = AnsA + 1;
db.Execute("UPDATE Results SET ResultsA=#0 WHERE ResultsId=#1", AnsA, Question);
}
i have also tried:
if (Request["prow.PollId"] == "B") {
int AnsB = row.ResultsB;
AnsB += 1;
db.Execute("UPDATE Results SET ResultsB=#0 WHERE ResultsId=#1", AnsB, prow.PollId);
}
Do you want to get value in form with dynamic inputs? If yes, you can try this:
NameValueCollection nvc = Request.Form;
foreach (var item in Request.Form.AllKeys)
{
//do something you want.
// Examble : if(item == "A")// item will return name of input
// Note: nvc[item] return value of input
}
Update:
Request.Form.AllKeys will return all of input name in form.
We use foreach to lopp through colections of input name.
Use nvc[item] or Request.Form[item] to get value of input.
You can read this article :c#: get values posted from a form

Categories

Resources