C# 小数点;将.ToInt转换为“;导致格式错误的语句?

C# 小数点;将.ToInt转换为“;导致格式错误的语句?,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我一直在遵循这一点,以帮助我创建一个基本的购物篮。当尝试运行添加了以下方法的代码时,应该计算订单上篮子的TotalPrice&TotalProducts,我得到以下错误: mscorlib.dll中出现“System.FormatException”,但未在用户代码中处理。其他信息:输入字符串的格式不正确 指着那条线 long ProductPrice = Convert.ToInt64(PriceLabel.Text) * Convert.ToInt64(ProductQu

我一直在遵循这一点,以帮助我创建一个基本的购物篮。当尝试运行添加了以下方法的代码时,应该计算订单上篮子的TotalPrice&TotalProducts,我得到以下错误:

mscorlib.dll中出现“System.FormatException”,但未在用户代码中处理。其他信息:输入字符串的格式不正确

指着那条线

long ProductPrice = 
    Convert.ToInt64(PriceLabel.Text) * 
    Convert.ToInt64(ProductQuantity.Text);
在数据库中,ProductPrice的一个示例是“199.99”,其中教程仅使用整数,例如“199”。我不确定这是否是导致问题的原因?如果是这种情况,那么应该如何修改下面的代码以包含小数点

我对编码还不熟悉,所以这真的让我很紧张,我真的很感激一些
guidence/建议的
代码更改来克服这个问题

谢谢大家

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);
}
pnlMyBasket

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

'runat=“server”id=“imgProductPhoto”style=“border:ridge 1px黑色;
宽度:157px;高度:130px;“/>
可用的密码:

价格:
所需数量x
pnlordeperform

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

姓名:
电话号码:

了解您当前工作方式的最直接方式:

 var ProductPrice = Convert.ToDecimal(PriceLabel.Text) * Convert.ToDecimal(ProductQuantity.Text);
如果无法将值转换为十进制,则会引发异常。Decimal.TryParse()会更好


我想你需要先转换成十进制,然后再转换成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);
}

可能重复的我已更改为这一行代码,但仍然收到相同的错误消息:(您是否已通过代码查看PriceLabel.Text和ProductQuantity.Text的值?我刚刚这样做,无论出于何种原因,它们都显示为“null”Arghhh!这是新手的问题!不知道为什么:(嗨@code4life…我也试过了,但在catch部分也出现了同样的错误:(我希望我能和比我更了解它的人分享我的屏幕!哈!嗯,有什么例外吗?也许两个文本框都是空的?只是猜测一下。顺便说一句,我在LinqPad中测试了这个,并验证了代码的有效性…啊,不可能!)(因此,应该从数据库中调用价格,首次添加到购物篮时,产品数量设置为1。我刚刚调试了代码,它说两个值都为0,但代码与教程视频完全相同!:(如果您想看一下,我很高兴与您分享我的屏幕。)(不确定这里是否允许这样做,如果不允许,请道歉)我想多花几分钟调试一下。我相信你会看到问题所在。我在银行防火墙后面,很难进一步交流…;-)我没有这方面的工作-只有一个问题。在完成数学运算后,放入TotalPrice值中的值仍然四舍五入到最接近的整数,并且不显示十进制值,例如349“。99”-有什么想法吗?干杯!