我总是收到以下错误:
“输入字符串的格式不正确。”

当我尝试将字符串从标签转换为整数时。我确定标签中有一个字符串。这是我的代码

C#

protected void btnBestel_Click1(object sender, EventArgs e)
{

    bestelling = new OrderBO();
    bestelling.Adress = txtAdress.Text;
    bestelling.Amount = Int32.Parse(lblAmount.Text);
    bestelling.BookID = bookID;

}


.aspx

<table width="650">
    <tr class="txtBox">
        <td>
            Boek
        </td>
        <td>
            Prijs
        </td>
        <td>
            Aantal
        </td>
        <td>
            Korting
        </td>
        <td>
            Totale Prijs
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="lblTitelBestel" runat="server" Text="" />
        </td>
        <td>
            <asp:Label ID="lblPriceBestel" runat="server" Text="" />
        </td>
        <td>
            <asp:TextBox ID="txtAdress" runat="server" Text="Belgium" />
        </td>
        <td>
            <asp:Label ID="lblKorting" runat="server" Text="-10%" />
        </td>
        <td>
            <asp:Label ID="lblAmount" runat="server" Text="20"/>
        </td>
    </tr>
</table>


我也尝试了Convert.ToInt32(lblAmount.Text);

我究竟做错了什么?

谢谢,
文森特

最佳答案

lblAmount.Text中的值不能转换为整数,即它包含非数字数据。

如果在行上设置断点:

bestelling = new OrderBO();


然后将鼠标悬停在lblAmount.Text上,它的值是多少?

值得一提的是Convert.ToInt32 is more forgiving,也许值得一试。尽管如果您期望lblAmount.Text中有一个有效的数值,那么更改为Convert.ToInt32并不是正确的解决方案。

关于c# - C#:输入字符串的格式不正确,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2865643/

10-12 12:42
查看更多