我有一個gridview將字符串轉換爲雙誤差錯OCCURS
只要我點擊一個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 [email protected]";
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();
}
CODE電網
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電網)錯誤:Input string was not in a correct format.
Line 255: totalAmount = int.Parse(dr["TotalAmount"].ToString());
ASP.NET碼
<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>
我試圖使它詮釋和小數點但不起作用
什麼是不完整的我的代碼?
請幫助球員。先謝謝你!仍然是新的c#
爲什麼不調試它來查看你在錯誤行上得到了什麼?也許沒有什麼不能轉換爲int的null。此外它更好地使用int.tryparse,但無論如何,其他一定是發送那裏空值的錯誤,或誰知道什麼。逐步調試調試調試。 – Aristos
@Aristos我一步一步調試它,併發生線錯誤huhuhu –
行錯誤發生,你看到裏面的值是什麼?請閱讀一些書籍,請其他人向您展示如何調試和解決問題。沒有人可以爲你調試,因爲你需要在你的電腦上找到這個錯誤。很多人也很難在他們的腦海中進行調試,但是這個錯誤很容易解決。你已經失去了更多的時間來編寫所有這些,而不是真正解決它。 – Aristos