Click here to Skip to main content
16,021,464 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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");
    }


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
Posted
Updated 24-Jun-14 23:43pm
v2

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.
 
Share this answer
 
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[^]
 
Share this answer
 
check the error what is coming from your database.
 
Share this answer
 
Comments
Member 10878414 25-Jun-14 4:47am    
i have already checked the database. Everything is fine in it. How can i use TryParse

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900