输入字符串的格式不正确。错误 [英] Input string was not in a correct format. ERROR

查看:100
本文介绍了输入字符串的格式不正确。错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Button1_Click(object sender, EventArgs e)
    {
        double Price = double.Parse(((Label)DataList1.Controls[0].FindControl("PriceLabel")).Text);
        string ProductName = ((Label)DataList1.Controls[0].FindControl("NameLabel")).Text;
        string ProductImageUrl = ((Label)DataList1.Controls[0].FindControl("ImageUrlLabel")).Text;
        int ProductID = int.Parse(Request.QueryString["ProductID"]);
        if (Profile.SCart == null)
        {
            Profile.SCart = new ShoppingCartExample.Cart();
        }
        Profile.SCart.Insert(ProductID, Price, 1, ProductName, ProductImageUrl);
        Server.Transfer("Products.aspx");
    }





每次我点击button1我都会收到错误的输入字符串在代码行中格式不正确

double Price = double.Parse(((Label)DataList1.Controls [0] .FindControl(PriceLabel))。Text)



我该如何解决?



Everytime i click on button1 i get an error of input string was not in correct format in code line
double Price = double.Parse(((Label)DataList1.Controls[0].FindControl("PriceLabel")).Text)

How can i fix it

推荐答案

让用户正确输入,或者(如果该Text属性来自您的数据库)确保它只包含有效数字。



考虑使用TryParse:如果返回bool,如果它不起作用则为false:然后你可以向用户报告一个明智的错误。
Get the user to type correctly, or (if that Text property is coming from your DB) make sure that it contains only valid numbers.

Consider using TryParse instead: if returns a bool which is false if it doesn't work: you can then report a sensible error to the user.


在解析为双之前请检查



if(((Label)DataList1.Controls [0] .FindControl(PriceLabel))。 Text!= String.Empty)

{

double Price = double.Parse(((Label)DataList1.Controls [0] .FindControl(PriceLabel))。文字)

}



http: //webxperts.co.in/questionsandanswers [ ^ ]
Before parse to double please check

if(((Label)DataList1.Controls[0].FindControl("PriceLabel")).Text!=String.Empty)
{
double Price = double.Parse(((Label)DataList1.Controls[0].FindControl("PriceLabel")).Text)
}

http://webxperts.co.in/questionsandanswers[^]


检查错误来自数据库的内容。
check the error what is coming from your database.


这篇关于输入字符串的格式不正确。错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆