Rhym
Rhym

Reputation: 33

Total Amount won't calculate and show the record in the textbox C#

I am currently doing a project similar to a sales and inventory. Where everytime you add a resource (materials, equipment, vehicle, contractor) it must calculate the total cost/amount based on the price in the database.

I have here a textbox where the total amount must display. Suppose to be that everytime you add a resource it should update. And when I click the Save button, it must be inserted in the database.

What happen in here is that it does calculate but then the total amount wont show in the current context. You have to press the Save button then you need to go back to that page before the record shows. Then If you want to Save it to the database you need to click the Save button again.

Below are my codes for getting the Total Amount


//Getting Total Cost Per Resources
decimal GetTotalMaterialCost()
{
    decimal total = 0;
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText =
        "SELECT SUM(rm.Quantity * m.SellingPrice) AS TotalMaterialCost FROM Resource_Materials rm " +
        "JOIN Materials m ON m.MaterialID = rm.MaterialID " +
        "JOIN ProjectTasks t ON t.TaskID = rm.TaskID " +
        "WHERE t.TaskID=@TaskID HAVING COUNT (*) > 0";
    cmd.Parameters.AddWithValue("@TaskID", Request.QueryString["ID"].ToString());
    object data = cmd.ExecuteScalar();
    if (data == null)
        total = 0;
    else
        total = (decimal)cmd.ExecuteScalar();
    con.Close();
    return total;
}

decimal GetTotalEquipmentCost()
{
    decimal total = 0;
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText =
        "SELECT SUM(e.Price * re.Quantity) AS TotalEquipmentCost FROM Resource_Equipments re " +
        "JOIN Equipments e ON e.EquipmentID = re.EquipmentID " +
        "JOIN ProjectTasks t ON t.TaskID = re.TaskID " +
        "WHERE t.TaskID=@TaskID HAVING COUNT (*) > 0";
    cmd.Parameters.AddWithValue("@TaskID", Request.QueryString["ID"].ToString());
    object data = cmd.ExecuteScalar();
    if (data == null)
        total = 0;
    else
        total = (decimal)cmd.ExecuteScalar();
    con.Close();
    return total;
}

decimal GetTotalVehicleCost()
{
    decimal total = 0;
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText =
        "SELECT SUM(rv.Cost) FROM Resource_Vehicles rv " +
        "JOIN Vehicles v ON v.VehicleID = rv.VehicleID " +
        "JOIN ProjectTasks t ON t.TaskID = rv.TaskID " +
        "WHERE t.TaskID=@TaskID HAVING COUNT (*) > 0";
    cmd.Parameters.AddWithValue("@TaskID", Request.QueryString["ID"].ToString());
    object data = cmd.ExecuteScalar();
    if (data == null)
        total = 0;
    else
        total = (decimal)cmd.ExecuteScalar();
    con.Close();
    return total;
}

decimal GetTotalContractorCost()
{
    decimal total = 0;
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText =
        "SELECT SUM(c.Rate) FROM Resource_Contractors rc " +
        "JOIN Contractors c ON c.ContractorID = rc.ContractorID " +
        "JOIN ProjectTasks t ON t.TaskID = rc.TaskID " +
        "WHERE t.TaskID=@TaskID HAVING COUNT (*) > 0";
    cmd.Parameters.AddWithValue("@TaskID", Request.QueryString["ID"].ToString());
    object data = cmd.ExecuteScalar();
    if (data == null)
        total = 0;
    else
        total = (decimal)cmd.ExecuteScalar();
    con.Close();
    return total;
}
//End

double GetAmount()
{
    double balance = 0;
    balance = Convert.ToDouble(GetTotalMaterialCost() + GetTotalEquipmentCost() + GetTotalVehicleCost() + GetTotalContractorCost());
    return balance;
} //Count Total Actual Cost

Below is the code i declare to show the record in the TextBox which I put in the Page_Load


ltAmount.Text = GetAmount().ToString("0.00");


Below is my source code for the TextBox


    <!--Cost-->
    <div class="form-group">
        <label class="control-label col-lg-4">
            Cost</label>
        <div class="col-lg-8">
            <asp:TextBox ID="ltAmount" runat="server" class="form-control" type="number" min="0.01"
                max="1000000000.00" step="0.01" ReadOnly />
        </div>
    </div>

If you notice I add a Resource Contractor with a Cost/Rate of 500.75. The 500.75 need to to show in the Cost TextBox but it does not show. It will only show when I pressed the Save Button and reopen the page again then thats the time I will be able to save it to the database by clicking the save button once more.


Problem


Here's my actual code for page. Click Here


Please tell me if you need clarifications. Thanks! Hoping for your kind answers.

Upvotes: 0

Views: 656

Answers (2)

psoshmo
psoshmo

Reputation: 1550

The issue here is that since you are setting the text in your page load event, it does not get refreshed until a post back occurs. As has been suggested, simply reset the text after you recalculate the cost. I do recommend you look up the page life cycle though, if you are unfamiliar with it. Here is a good summary from the MSDN website. https://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx

Upvotes: 1

Rhym
Rhym

Reputation: 33

"You mean after adding resource you should be able to display total amount? If yes then ltAmount.Text = GetAmount().ToString("0.00"); after adding resourse code." – Imadoddin Ibn Alauddin

Upvotes: 1

Related Questions