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

时间:2015-07-14 04:15:16

标签: c# asp.net sql-server-2012

我目前正在做一个类似于销售和库存的项目。每次添加资源(材料,设备,车辆,承包商)时,必须根据数据库中的价格计算总成本/金额。

我这里有一个文本框,其中必须显示总金额。假设每次添加资源时它都应该更新。当我单击“保存”按钮时,必须将其插入数据库中。

这里发生的事情是它确实计算但是总量不会在当前上下文中显示。您必须按“保存”按钮,然后您需要在记录显示之前返回该页面。然后,如果要将其保存到数据库,则需要再次单击“保存”按钮。

以下是我获取总金额的代码

//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

下面是我声明要在TextBox中显示记录的代码,我在Page_Load

ltAmount.Text = GetAmount()。ToString(“0.00”);

下面是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>

如果您发现我添加了资源承包商,其成本/费率为500.75。 500.75需要在Cost TextBox中显示,但它不显示。它只会在我按下“保存”按钮并再次重新打开页面时显示,那时我可以再次单击“保存”按钮将其保存到数据库中。

Problem

Here's my actual code for page. Click Here

如果您需要澄清,请告诉我。谢谢!希望得到你的答复。

2 个答案:

答案 0 :(得分:1)

“你的意思是在添加资源后你应该能够显示总金额吗?如果是,那么ltAmount.Text = GetAmount()。ToString(”0.00“);在添加资源代码之后。” - Imadoddin Ibn Alauddin

答案 1 :(得分:1)

这里的问题是,由于您在页面加载事件中设置了文本,因此在发生回发之前不会刷新文本。如上所述,只需在重新计算成本后重置文本即可。如果您不熟悉它,我建议您查看页面生命周期。以下是MSDN网站的一个很好的总结。 https://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx