locked
Struggling with simple calculations RRS feed

  • Question

  • User-1874496678 posted

    Hi

    I have a form which serves as basket for donations. It is working fine but would like to do simple calculations on total amount, but struggling with it.

    I get total from query

     sql = "SELECT Sum(Cart.Amount) AS TotalAmount FROM Cart WHERE Cart.SessionId = @0";
     var Total = db.Query(sql, sessionid);

    which is then displayed as table footer.

               @foreach (var item in Total)
                {
                <tr >
                    <td style=" border-top:2px solid #C2EBFF;" >&nbsp;</td>
                    <td style=" border-top:2px solid #C2EBFF;" >Total: </td>
                    <td style=" border-top:2px solid #C2EBFF;" >£@item.TotalAmount</td>
     
                </tr>
                }
    

    This is ok. Now I am trying to display simple calculations regarding difference in transaction charges between Debit (e.g. multiply by 0.025) and Credit card (e.g. multiply by 0.3) but getting errors if I perform any calculations on TotalAmount.

    Any help is much appreciated.

    Also I am not sure why I have to use a loop with var Total, as only one record is returned, if I use QuerySingle, I get error. any simple explanation would help my understanding.

    Regards

    Hamid

    Tuesday, May 31, 2016 5:35 PM

Answers

  • User-821857111 posted

    You should post the errors you get. It will help to diagnose what you are doing wrong.

    Also I am not sure why I have to use a loop with var Total, as only one record is returned, if I use QuerySingle, I get error. any simple explanation would help my understanding.
    Ideally, you should use QueryValue, as that is designed to cater for retrieving scalar values:

    sql = "SELECT Sum(Cart.Amount) AS TotalAmount FROM Cart WHERE Cart.SessionId = @0";
    var TotalAmount = db.QueryValue(sql, sessionid);
    <tr >
       <td style=" border-top:2px solid #C2EBFF;" >&nbsp;</td>
       <td style=" border-top:2px solid #C2EBFF;" >Total: </td>
       <td style=" border-top:2px solid #C2EBFF;" >£@TotalAmount</td>

    No looping - just a single value. QuerySingle is for dealing with single row results.

    If you want to perform mathematical calculations on TotalAmount, you may well have to cast it to a decimal.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 31, 2016 8:12 PM
  • User325035487 posted
    item.TotalAmount is a dynamic data type
    do this.
    var Total = db.QueryValue(sql, sessionid);
    if(Total!=null){
    var totalvalue = (float)Total;
    //now u can do calculations on totalvalue
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 1, 2016 12:04 PM

All replies

  • User-821857111 posted

    You should post the errors you get. It will help to diagnose what you are doing wrong.

    Also I am not sure why I have to use a loop with var Total, as only one record is returned, if I use QuerySingle, I get error. any simple explanation would help my understanding.
    Ideally, you should use QueryValue, as that is designed to cater for retrieving scalar values:

    sql = "SELECT Sum(Cart.Amount) AS TotalAmount FROM Cart WHERE Cart.SessionId = @0";
    var TotalAmount = db.QueryValue(sql, sessionid);
    <tr >
       <td style=" border-top:2px solid #C2EBFF;" >&nbsp;</td>
       <td style=" border-top:2px solid #C2EBFF;" >Total: </td>
       <td style=" border-top:2px solid #C2EBFF;" >£@TotalAmount</td>

    No looping - just a single value. QuerySingle is for dealing with single row results.

    If you want to perform mathematical calculations on TotalAmount, you may well have to cast it to a decimal.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 31, 2016 8:12 PM
  • User325035487 posted
    item.TotalAmount is a dynamic data type
    do this.
    var Total = db.QueryValue(sql, sessionid);
    if(Total!=null){
    var totalvalue = (float)Total;
    //now u can do calculations on totalvalue
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 1, 2016 12:04 PM