Click here to Skip to main content
15,918,193 members
Please Sign up or sign in to vote.
1.40/5 (3 votes)
See more:
Can anyone know?

Input string was not in a correct format.how to solve?

C#
protected void totalprice()
    {
        int quantity = int.Parse("txtquantity");
     float price =float.Parse("price");
     float totalprice = float.Parse("totalPrice");
     totalprice=quantity * price;
}
Posted
Updated 16-Oct-17 4:21am
Comments
Prince Antony G 3-Dec-11 1:00am    
which line of code gives error..

hope txtquantity,price,totalPrice are variabeles
C#
int quantity = int.Parse(txtquantity);
    float price =float.Parse(price);
    float totalprice = float.Parse(totalPrice);
 
Share this answer
 
Comments
Uday P.Singh 3-Dec-11 0:35am    
5ed :)
uspatel 3-Dec-11 0:50am    
thanks...
You probably mean the following (where price, totalprice and txtquantity are objects):
C#
protected void totalprice()
    {
        int quantity = int.Parse(txtquantity.Text);
     float price =float.Parse(price);
     float totalprice = float.Parse(totalPrice);
     totalprice=quantity * price;
}
 
Share this answer
 
Comments
Uday P.Singh 3-Dec-11 0:40am    
5ed :)
Mehdi Gholam 3-Dec-11 5:08am    
Thanks
You need to learn the Type casting in C#. Have a look at these:

http://msdn.microsoft.com/en-us/library/ms173105.aspx[^]

http://msdn.microsoft.com/en-us/library/ms173105%28v=vs.80%29.aspx[^]

hope it helps :)
 
Share this answer
 
Comments
Mehdi Gholam 3-Dec-11 5:09am    
5'ed
Uday P.Singh 3-Dec-11 11:29am    
thank you :)
Using Int.TryParse or Float.TryParse is better than casting or converting the output values directly.

This error occurs when trying to convert a value which is not convertible to the desired datatype.

Regards,
Eduard
 
Share this answer
 
how can you convert string value "txtquantity" in to integer..?? and "price" into float, and "totalPrice" into float.

if these are text boxes then try this..

C#
protected void totalprice()
    {
try{
        int quantity  = int.Parse(txtquantity.Text);
     float price =float.Parse(price.Text);
     float totalprice =   quantity * price;
}
catch(Exception e){ //output e to see}
    }


if these are string variables then, try this..

C#
protected void totalprice()
    {
try
{
        int quantity  = int.Parse(txtquantity);
     float price =float.Parse(price);
     float totalprice = quantity * price;
} catch(Exception e) { //output e }
    }
 
Share this answer
 
v3
I don't understand your problem.You are trying to Convert a String Value into Integer and trying to store it into Integer Variable which is not possible.If txtquantity is the TextBox then you have to save the TextBox value into the Variable befor Calculating Total Price.See this code.
C#
int _quantity = int.Parse(txtQuantity.Text);
float _price = float.Parse(txtPrice.Text);
float _totalPrice = _quantity * _price;
txtTotalPrice.Text = _totalPrice;

This is the proper way to perform calculation.If you want somthing another output then please explain your question in brief.

-MKB
 
Share this answer
 
How do you want to convert string "txtquantity" to an integer value ?

Maybe you should use something like Request.Parameters["txtquantity"].ToString() or Session["txtquantity"].ToString() or txtquntity.Text .

So try to identify your source of values more carefully.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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