Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
I have a little issue with adding values from two textboxes.

textbox 1 = gets value from sql (ex. 24.00 or 2.00 or 4.00,...)
textbox 2 = user enters value (ex. 24.00 or 2.00 or 4.00,...)

This is the error i get:
Input string was not in a correct format.


I have tried many different things, convert, decimal, tryparse, and i cant figuer out what i can do to fix this.

Please help.
Thank You.

UPDATE: fixed the values. Added "or" where i had ",".

C#
int totalAccwPrev = Convert.ToInt32(txtPrevRecd.Text) + Convert.ToInt32(txtAQty.Text);
int qtyOrd = Convert.ToInt32(txtQtyOrd.Text);

if (totalAccwPrev > qtyOrd)
{
    if (ddRCodeList.SelectedValue == "")
    {
        ShowPopUpMsg("Please select appropriate Rejected Code.");
        return;
    }
}
Posted
Updated 9-Dec-15 4:53am
v4
Comments
Anisuzzaman Sumon 9-Dec-15 10:08am    
Please show your code
r00n3y 9-Dec-15 10:54am    
added the code.
Sergey Alexandrovich Kryukov 9-Dec-15 10:14am    
If your input string cannot be parsed as some numeric data, it cannot be used for your arithmetic. What may possibly need help here?
—SA
F-ES Sitecore 9-Dec-15 10:18am    
You're going to have to post the code and also clarify exactly what is in your text boxes. Eg is it

24.00

or it could maybe be

2.00

or do you mean it is literally

24.00,2.00,4.00

Anyway, assuming it is a single value like 2.00 and assuming you are using TryParse, if you're getting errors it is probably to do with globalisation. 2.00 will only convert to 2 when using cultures that use a period as a decimal separator. So maybe you need to explicitly state the culture you want your TryParse to use (it's one of the parameters).
r00n3y 9-Dec-15 10:54am    
updated the question.

If your database is returning values which are literally
C#
24.00,2.00,4.00

Then you have multiple values, and you can't store those in a single variable -= you need a separate one for each number in the sequence.
One way to handle this is to use Split:
C#
string s1 = "24.00,2.00,4.00";
string s2 = "14.00,12.00,8.00";
string[] parts1 = s1.Split(',');
string[] parts2 = s2.Split(',');
for (int i = 0; i < Math.Min(parts1.Length, parts2.Length); i++)
    {
    float f1, f2;
    if (float.TryParse(parts1[i], out f1) && float.TryParse(parts2[i], out f2))
        {
        Console.WriteLine("{0} + {1} = {2}", f1, f2, f1 + f2);
        }
    }

But you need to look at your DB and change it to not store comma separated values!
 
Share this answer
 
Comments
r00n3y 9-Dec-15 10:41am    
Updated the question. Values in db are not stored with "," its either 24.00 or 2.00 or xx.xx
OriginalGriff 9-Dec-15 11:03am    
So what code did you try that didn't work?
The code I gave will work for strings, just rip out the split related bits and it'll still work...
r00n3y 9-Dec-15 11:53am    
Thank you, i was able to modify the parts section and was able to get the result i wanted.
OriginalGriff 9-Dec-15 12:01pm    
You're welcome!
ridoy 9-Dec-15 14:23pm    
5ed!
Hello..

Try to first convert textboxes value into decimal then into integer...

C#
int totalAccwPrev = Convert.ToInt32(Convert.ToDecimal("5.00") + Convert.ToDecimal("2.00"));
           int qtyOrd = Convert.ToInt32(Convert.ToDecimal("4.00"));


convert to integer get only integer part from the decimal i.r value before '.'

Hope this helps.
Thanks
 
Share this answer
 
C#
textBox3.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text).ToString());
 
Share this answer
 

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