我有一个gridview
每当我点击一个btn时,都会在gridview中插入并查看。
我的问题是我无法得到金额的总和
当我为gridview添加值以显示
时我有一个获取金额总和的代码
void GetTotalAmount()
{
int GetRefNo;
decimal GetUnitPrice;
//GetCart();
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT SUM(Amount) AS TotalAmount " +
"FROM PRDetails WHERE POID=@POID";
cmd.Parameters.AddWithValue("@UserID", Session["userid"].ToString());
cmd.Parameters.AddWithValue("@POID", POID.Text);
double totalAmount = 0;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
totalAmount = int.Parse(dr["TotalAmount"].ToString());
}
lbSubTotal.Text = (totalAmount * .88).ToString("#,###.00");
cmd.Parameters.AddWithValue("totalAmount", totalAmount.ToString());
con.Close();
}
else
{
lbSubTotal.Text = "0.00";
}
//
con.Close();
}
网格代码
protected void AddProduct_Click(object sender, EventArgs e)
{
decimal price = GetPrice(ID);
//GetTotalAmount();
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "INSERT INTO PRDetails VALUES (@ProductID, @ProductName, @Price, @Quantity, @Amount, @POID)";
cmd.Parameters.AddWithValue("@UserID", Session["userid"].ToString());
cmd.Parameters.AddWithValue("@ProductID", lbProductID.Text);
cmd.Parameters.AddWithValue("@ProductName", ddlName.SelectedValue);
cmd.Parameters.AddWithValue("@Price", decimal.Parse(lbPrice.Text));
cmd.Parameters.AddWithValue("@Quantity", Quantity.Text);
cmd.Parameters.AddWithValue("@Amount", int.Parse(Quantity.Text) * price);
cmd.Parameters.AddWithValue("@POID", POID.Text);
cmd.ExecuteNonQuery();
GridView1.DataBind();
con.Close();
每当我添加产品(btn for grid)时,都会出现ERROR:Input string was not in a correct format.
Line 255: totalAmount = int.Parse(dr["TotalAmount"].ToString());
ASP.NET CODES
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="1000px" AllowPaging="True" HorizontalAlign="Center" PageIndex="20"
ShowFooter="True" DataSourceID="details" >
<Columns>
<asp:BoundField DataField="POID" HeaderText="POID"
SortExpression="POID" />
<asp:BoundField DataField="ProductID" HeaderText="ProductID"
SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName"
SortExpression="ProductName" />
<asp:BoundField DataField="Price" HeaderText="Price"
SortExpression="Price" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity"
SortExpression="Quantity" />
<asp:BoundField DataField="Amount" HeaderText="Amount"
SortExpression="Amount" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="details" runat="server"
ConnectionString="<%$ ConnectionStrings:MyOwnMeatshopConnectionString %>"
SelectCommand="SELECT [POID], [ProductID], [ProductName], [Price], [Quantity], [Amount] FROM [PRDetails] WHERE ([POID] = @POID)">
<SelectParameters>
<asp:ControlParameter ControlID="POID" Name="POID" PropertyName="Text"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<div class="container">
<div class="pull-right col-lg-5">
<h5 style="font-weight: bold" />SUB TOTAL Php <asp:Label ID="lbSubTotal" runat="server"></asp:Label>
我尝试将其设为int和decimal但不起作用
我的代码不完整?
请帮助伙计们。先感谢您!仍然是c#
的新手答案 0 :(得分:0)
您可以更改小数价格= GetPrice(ID);加倍价格= GetPrice(ID); 对不起,我没有足够的代表发表评论。