Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I m using the following code to calculate average purchase prize.I have two classes PurchaseAccountBLL and StockpageBLL. The unit_prize,Quantity, Item_discount is binding from purchase database and Stock_Item_Quantity ,Stock_ItemUnit_Prize,Profit is coming from Stock database table. So I want to calculate the following equation: Profit= (unit_prize*Quantity)-(Stock_Item_Quantity*Stock_ItemUnit_Prize) and update the updated data into stock database table. How I can achieve this.
Thanks in advace

This is my code:


I m retrieve database table data in a list


public List<purchaseaccountbll> PurchaseAccountSelection()
{
DataTable dt = prl.SelectPurchaseAccount();
List<purchaseaccountbll> purchase = new List<purchaseaccountbll>();
foreach (DataRow row in dt.Rows)
{
PurchaseAccountBLL a = new PurchaseAccountBLL();
a.Itemname = row["item_name"].ToString();
a.Id = Convert.ToInt32(row["purchase_id"]);

a.Unit_Prize = Convert.ToDecimal(row["unit_prize"]);
a.Quantity = Convert.ToDecimal(row["quantity"]);
a.Item_Discount = Convert.ToDecimal(row["discount"]);



purchase.Add(a);
}
return purchase;
}


public List<stockpagebll> StockSelection()
{
DataTable dt = prl.StockSelection();
List<stockpagebll> stock = new List<stockpagebll>();
decimal total_prize = 0;
foreach (DataRow row in dt.Rows)
{
StockpageBLL add = new StockpageBLL();
add.Id = Convert.ToInt32(row["item_id"]);
add.Itemname = Convert.ToString(row["itemname"]);
add.Stock_Item_Quantity = Convert.ToDecimal(row["quantity"]);
add.Stock_Barcode = Convert.ToString(row["barcode"]);
add.Stock_ItemUnit_Prize = Convert.ToDecimal(row["unitPrize"]);
add.Profit = Convert.ToDecimal(row["profit"]);

total_prize += Convert.ToDecimal(row["unit_prize"]);
// StockpageDLL stk = new StockpageDLL(Profit,Id);
stock.Add(add);

}
decimal avg = total_prize / dt.Rows.Count;
StockpageDLL stk = new StockpageDLL(avg, barcode);
return stock;


}
Posted
Comments
Gyaneswar kumar 4-Nov-15 23:21pm    
what error you're getting or please let us know problem here..

1 solution

Just like my previous solution to you,
Profit= (unit_prize*Quantity)-(Stock_Item_Quantity*Stock_ItemUnit_Prize)

C#
decimal purchaseTotal = 0;
foreach (DataRow row in dt.Rows)
{
    PurchaseAccountBLL a = new PurchaseAccountBLL();
    a.Itemname = row["item_name"].ToString();
    a.Id = Convert.ToInt32(row["purchase_id"]);
    
    a.Unit_Prize = Convert.ToDecimal(row["unit_prize"]);
    a.Quantity = Convert.ToDecimal(row["quantity"]);
    a.Item_Discount = Convert.ToDecimal(row["discount"]);
    purchase.Add(a);

    purchaseTotal += (Convert.ToDecimal(row["unit_prize"]) * Convert.ToDecimal(row["quantity"]));
}

decimal stockTotal = 0;
foreach (DataRow row in dt.Rows)
{
    StockpageBLL add = new StockpageBLL();
    add.Id = Convert.ToInt32(row["item_id"]);
    add.Itemname = Convert.ToString(row["itemname"]);
    add.Stock_Item_Quantity = Convert.ToDecimal(row["quantity"]);
    add.Stock_Barcode = Convert.ToString(row["barcode"]);
    add.Stock_ItemUnit_Prize = Convert.ToDecimal(row["unitPrize"]);
    add.Profit = Convert.ToDecimal(row["profit"]);

    stockTotal += (Convert.ToDecimal(row["quantity"]) * Convert.ToDecimal(row["unit_prize"]));

    total_prize += Convert.ToDecimal(row["unit_prize"]);
    // StockpageDLL stk = new StockpageDLL(Profit,Id);
    stock.Add(add);
}

    decimal profit = purchaseTotal -  stockTotal; 


-KR
 
Share this answer
 
Comments
BillWoodruff 5-Nov-15 0:37am    
If you have already posted this code in response to a previous question, why post it again ? Note: I'm not voting on this solution.
Member 12115818 5-Nov-15 10:37am    
In the above code, how to update data into the database..?
and how to get the stockpageBLL class field into purchaseAccountBLL class..

Please help me I'm waiting for the solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900