“Convert.ToInt”语句中的小数点导致格式错误?

时间:2016-03-04 19:03:36

标签: c# asp.net sql-server

我一直关注这个tutorial来帮助我创建一个基本的购物篮。尝试使用以下方法运行代码时,应该计算TotalPrice& OrderForm上篮子的TotalProducts,我得到以下错误:

  

'System.FormatException'发生在mscorlib.dll中,但未在用户代码中处理。附加信息:输入字符串的格式不正确。

指着

guideance/suggested

在数据库中,ProductPrice的示例是“199.99”,因为本教程仅使用整数,例如“199”。我不确定这是否是造成这个问题的原因?如果是这种情况,下面的代码应该如何修改以包含小数点?

我是编码的新手,所以这真的让我很紧张,我真的很感激一些private void UpdateTotalBill() { long TotalPrice = 0; long TotalProducts = 0; foreach (DataListItem item in dlBasketItems.Items) { //Finds the price label on pnlMyBasket Label PriceLabel = item.FindControl("lblPrice") as Label; //Finds the quantity text box on pnlMyBasket TextBox ProductQuantity = item.FindControl("txtProductQuantity") as TextBox; long ProductPrice = Convert.ToInt32(PriceLabel.Text) * Convert.ToInt32(ProductQuantity.Text); TotalPrice = TotalPrice + ProductPrice; TotalProducts = TotalProducts + Convert.ToInt32(ProductQuantity.Text); } txtTotalPrice.Text = Convert.ToString(TotalPrice); txtTotalProducts.Text = Convert.ToString(TotalProducts); } 代码更改来克服这个问题

非常感谢你们!

 <asp:Panel ID="pnlMyBasket" runat="server" ScrollBars="Auto" Height="500px" BorderColor="Black" BorderStyle="Inset" BorderWidth="1px" Visible="false">
    <table align="center" cellspacing="1">
        <tr>
            <td align="center" class="auto-style4">
                <asp:Label ID="lblAvailableStockAlert" runat="server" ForeColor="Red" Font-Bold="true"></asp:Label>
                <asp:DataList ID="dlBasketItems" runat="server" RepeatColumns="3" Font-Bold="False"
                    Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False"
                    Width="551px">
                    <ItemTemplate>
                        <div align="left">
                            <table cellspacing="1" style="border: 1px ridge #9900FF; text-align: center; width: 172px;">
                                <tr>
                                    <td style="border-bottom-style: ridge; border-width: 1px; border-color: #000000">
                                        <asp:Label ID="lblProductName" runat="server" Text='<%# Eval("ProductName")%>' Style="font-weight: 700"></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <img alt="" src='<%# Eval("ProductImageUrl") %>' runat="server" id="imgProductPhoto" style="border: ridge 1px black;
                                            width: 157px; height: 130px;" />
                                    </td>
                                </tr>
                                <tr>
                                     <td>AvailableStock:&nbsp;                                                                        <asp:Label ID="lblAvailableStock" runat="server" Text='<%# Eval("ProductStock") %>'
                                            ToolTip="Available Stock" ForeColor="Red" Font-Bold="true"></asp:Label>
                                        <br />
                                         Price:<asp:Label ID="lblPrice" runat="server" Text='<%# Eval ("ProductPrice") %>'></asp:Label>
                                        &nbsp;<br /> Quantity Required x&nbsp;
                                        <asp:TextBox ID="txtProductQuantity" runat="server" Width="20px" Height="10px" MaxLength="2" OnTextChanged="txtProductQuantity_TextChanged" AutoPostBack="true" ></asp:TextBox>
                                        <asp:HiddenField ID="hfProductID" runat="server" Value='<%# Eval("ProductID") %>' />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <hr />                                                                        <asp:Button ID="btnRemoveFromBasket" runat="server" CommandArgument='<%# Eval("ProductID") %>'
                                            Text="Remove From Basket" Width="100%" BorderColor="Black" BorderStyle="Inset" BorderWidth="1px"
                                            OnClick="btnRemoveFromBasket_Click" CausesValidation="false" />

                                    </td>
                                </tr>
                            </table>
                        </div>
                    </ItemTemplate>
                    <ItemStyle Width="33%" />
                </asp:DataList>
                </td>
            </tr>
        </table>
    </asp:Panel>

pnlMyBasket

    <asp:Panel ID="pnlOrderForm" runat="server" ScrollBars="Auto" Height="500px" BorderColor="Black"
                                    BorderStyle="Inset" BorderWidth="1px" Visible="false">
                                    <table style="width: 258px;">
                                        <tr>
                                            <td align="left">
                                                Name:
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <asp:TextBox ID="txtCustomerName" runat="server" Width="231px"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtCustomerName"
                                                    ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="left">
                                                PhoneNo:
                                            </td>
                                        </tr
                                        <tr>
                                            <td>
                                                <asp:TextBox ID="txtCustomerPhoneNo" runat="server" Width="231px" MaxLength="10"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtCustomerPhoneNo"
                                                    ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="left">
                                                EmailID
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <asp:TextBox ID="txtCustomerEmailID" runat="server" Width="231px"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtCustomerEmailID"
                                                    ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="left">
                                                Address
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="left">
                                                &nbsp;<asp:TextBox ID="txtCustomerAddress" runat="server" Width="227px" Height="81px"
                                                    TextMode="MultiLine"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtCustomerAddress"
                                                    ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="left">
                                                Total Products :
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="center">
                                                &nbsp;<asp:TextBox ID="txtTotalProducts" runat="server" ReadOnly="True" Width="231px"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtTotalProducts"
                                                    ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="left">
                                                Total Price :
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <asp:TextBox ID="txtTotalPrice" runat="server" ReadOnly="True" Width="231px"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="txtTotalPrice"
                                                    ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="left">
                                                Payment Mode:
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="left">
                                                <asp:RadioButtonList ID="rblMethodOfPayment" runat="server">
                                                        <asp:ListItem Value="1" Selected="True">1. Collect & Pay In Store</asp:ListItem>
                                                        <asp:ListItem Value="2">2. Pay with card</asp:ListItem>
                                                        </asp:RadioButtonList>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <br />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <asp:Button ID="btnPlaceOrder" runat="server" OnClick="btnPlaceOrder_Click" Style="font-weight: 700"
                                                    Text="PlaceOrder" Width="90px" />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtCustomerEmailID"
                                                    ErrorMessage="Please Enter Valid EmailId" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
                                            </td>
                                        </tr>
                                    </table>
                                </asp:Panel>

pnlOrderForm

class Sites(models.Model):
    site_id = models.AutoField(primary_key=True)
    status = models.ForeignKey(Statuses, models.DO_NOTHING, blank=True, null=True)

2 个答案:

答案 0 :(得分:0)

您目前正在做的最直接的方式:

 var ProductPrice = Convert.ToDecimal(PriceLabel.Text) * Convert.ToDecimal(ProductQuantity.Text);

如果无法将值转换为小数,则会抛出异常。 Decimal.TryParse()会更好。

https://msdn.microsoft.com/en-us/library/system.decimal.tryparse(v=vs.110).aspx

答案 1 :(得分:0)

我认为您需要转换为十进制,然后转换为long。像这样:

long ProductPrice;
try
{
    // hey you never know, someone might just put in an int64 in that textbox!
    ProductPrice = Convert.ToInt32(PriceLabel.Text) * 
        Convert.ToInt32(ProductQuantity.Text);
}
catch
{
    decimal tempPrice = Convert.ToDecimal(PriceLabel.Text) *
        Convert.ToDecimal(ProductQuantity.Text);
    ProductPrice = Convert.ToInt64(tempPrice);
}