简体   繁体   English

总金额不会计算并在C#文本框中显示记录

[英]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 下面是我声明的代码,用于显示我在Page_Load中放入的TextBox中的记录


ltAmount.Text = GetAmount().ToString("0.00"); 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. 如果您注意到我添加的资源承包商的成本/费率为500.75。 The 500.75 need to to show in the Cost TextBox but it does not show. 500.75需要显示在“成本”文本框中,但不显示。 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. 仅当我按下“保存”按钮并再次重新打开页面时,它才会显示,这就是我再次单击“保存”按钮将其保存到数据库的时间。


问题


Here's my actual code for page. 这是我的页面实际代码。 Click Here 点击这里


Please tell me if you need clarifications. 请告诉我您是否需要澄清。 Thanks! 谢谢! Hoping for your kind answers. 希望能得到您的答复。

"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." “您的意思是,添加资源后,您应该能够显示总额?如果是,那么在添加资源代码之后,ltAmount.Text = GetAmount()。ToString(“ 0.00”);” – Imadoddin Ibn Alauddin – Imadoddin Ibn Alauddin

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. 这是MSDN网站上的一个很好的摘要。 https://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx https://msdn.microsoft.com/zh-CN/library/ms178472(v=vs.100).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM