Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding numbers in .net

Status
Not open for further replies.

jakeoo7

MIS
Oct 11, 2002
2
US
I am tring to take a number value in several textboxes and add them together. I keep getting: System.FormatException: Input string was not in a correct format.
Any Help would be greatly appreciated
This is my code:


Dim num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12, num13, num14, num15, num16, num17, num18, num19, num20, num21, num22, num23, num24, num25, num26, num27, num28, num29, num30, num31, num1o, num2o, num3o, num4o, num5o, num6o, num7o, num8o, num9o, num10o, num11o, num12o, num13o, num14o, num15o, num16o, num17o, num18o, num19o, num20o, num21o, num22o, num23o, num24o, num25o, num26o, num27o, num28o, num29o, num30o, num31o As Single
num1 = CSng(r1.Text)
num2 = CSng(r2.Text)
num3 = CSng(r3.Text)
num4 = CSng(r4.Text)
num5 = CSng(r5.Text)
num6 = CSng(r6.Text)
num7 = CSng(r7.Text)
num8 = CSng(r8.Text)
num9 = CSng(r9.Text)
num10 = CSng(r10.Text)
num11 = CSng(r11.Text)
num12 = CSng(r12.Text)
num13 = CSng(r13.Text)
num14 = CSng(r14.Text)
num15 = CSng(r15.Text)
num16 = CSng(r16.Text)
num17 = CSng(r17.Text)
num18 = CSng(r18.Text)
num19 = CSng(r19.Text)
num20 = CSng(r20.Text)
num21 = CSng(r21.Text)
num22 = CSng(r22.Text)
num23 = CSng(r23.Text)
num24 = CSng(r24.Text)
num25 = CSng(r25.Text)
num26 = CSng(r26.Text)
num27 = CSng(r27.Text)
num28 = CSng(r28.Text)
num29 = CSng(r29.Text)
num30 = CSng(r30.Text)
num31 = CSng(r31.Text)
num1o = CSng(o1.Text)
num2o = CSng(o2.Text)
num3o = CSng(o3.Text)
num4o = CSng(o4.Text)
num5o = CSng(o5.Text)
num6o = CSng(o6.Text)
num7o = CSng(o7.Text)
num8o = CSng(o8.Text)
num9o = CSng(o9.Text)
num10o = CSng(o10.Text)
num11o = CSng(o11.Text)
num12o = CSng(o12.Text)
num13o = CSng(o13.Text)
num14o = CSng(o14.Text)
num15o = CSng(o15.Text)
num16o = CSng(o16.Text)
num17o = CSng(o17.Text)
num18o = CSng(o18.Text)
num19o = CSng(o19.Text)
num20o = CSng(o20.Text)
num21o = CSng(o21.Text)
num22o = CSng(o22.Text)
num23o = CSng(o23.Text)
num24o = CSng(o24.Text)
num25o = CSng(o25.Text)
num26o = CSng(o26.Text)
num27o = CSng(o27.Text)
num28o = CSng(o28.Text)
num29o = CSng(o29.Text)
num30o = CSng(o30.Text)
num31o = CSng(o31.Text)
Dim total1r as Single
Dim total2r as Single
Dim total1o as Single
Dim total2o as Single
total1r = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10 + num11 + num12 + num13 + num14 + num15
total2r = num16 + num17 + num18 + num19 + num20 + num21 + num22 + num23 + num24 + num25 + num26 + num27 + num28 + num29 + num30 + num31
total1o = num1o + num2o + num3o + num4o + num5o + num6o + num7o + num8o + num9o + num10o + num11o + num12o + num13o + num14o + num15o
total2o = num16o + num17o + num18o + num19o + num20o + num21o + num22o + num23o + num24o + num25o + num26o + num27o + num28o + num29o + num30o + num31o
rtotal1.text = total1r
rtotal2.text = total2r
ototal1.text = total1o
ototal2.text = total2o
 
Most likely one of the Text boxes doesn has a character in it. Try using the debugger to see where your code breaks. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
GOOD GOD! how many textboxes doyou have on the screen?!

thats massive...ok, if you're getting a format exception, the most common problem is that you have one of those textboxes containing something that isn't a number (maybe an extra decimal, or maybe there's a $ sign in there somewhere, or someone entered a letter, etc.)

There's a couple of things that could solve your error:
1. use validators. you'd need an aweful lot of em, but that way you'd also get a visual of which textboxes might have invalid data
2. write some code that loops throgh all the textboxes on the page and validates them. That way you don't have 30+ validators on your page, and you can output an error message to the screen if you find the problem
3. GET RID OF ALL THOSE TEXTBOXES! Do they all have to be there? If its a case of people entering data over and over again, could you have a gui that has one textbox, and as they enter related amounts, it tabulates it, but clears the textbox? that way you wouldn't have so many controls.

Just a few thoughts anyway, but to your original question: you just have bad data somewhere.

D'Arcy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top