Last post Dec 05, 2007 09:02 PM by nomadic1
Dec 04, 2007 10:28 PM|nomadic1|LINK
Does anyone know how to simply add textbox controls together so that txt1.text + txt2.text + txt3.text + txt4.text equals txt5.text. I have done it with datgrids but I keep throwing an exception "Conversion from string "" to type 'Integer' is not valid."
Here is the code so far using only 2 textboxes ... I also have the same or similar errors trying to add 3 or more textboxes together:
LR1 = txtLaborRate1.Text
LR2 = txtLaborRate2.Text
LH1 = txtLaborHrs1.Text
LH2 = txtLaborHrs2.Text
LS1 = txtLaborSub1.Text
LS2 = txtLaborSub2.Text
LT = txtLaborTotal.Text
LT = Math.Round(LS1 + LS2, 2)
txtLaborTotal.Text = txtLaborSub1.Text
txtLaborTotal.Text = txtLaborSub2.Text
Thanks for any help. This is driving me nuts because I know it is something simple but I can't find an answer anywhere.
Dec 04, 2007 11:34 PM|DavidKiff|LINK
Hey my suggestion would be to create a function to return the integer that the textbox contains:
Then in your button click:
Dec 04, 2007 11:38 PM|DavidKiff|LINK
You have to remember that TextBox1.Text is actually a string so when you perform math fucntions on a string you wil get an error. You need to perform math functions on numbers (integers, floats, doubles). The .NET Framwork has some cool helper classes:
Dim theInt As Integer = Convert.ToInt32(TextBox1.Text)
the method i used was tryParse which returns true if it can convert it and false if it cant. It also excepts an 'out' parameter in the method so that the converted value can be accessed.
Dec 05, 2007 09:17 AM|cykophysh|LINK
the other guys have provided the answer. but to be sure. You need to preform validation on the text boxes that the user has entered a valid number. The int.Typarse , will only try to parse the integer, but it will not inform you whether the it was a
success, so potentially your user could misinterperet your form and insert $123 + $2003 + £250 . Which is valid , monetary values. and will curse you when it all adds up to 0.
So you'll need to validate the input, this can easily be accomplished by using the ASP.net validators, but always ensure it passes with some server side validation too.
Well that is my 2 pennies worth. [:D]
Dec 05, 2007 04:03 PM|DavidKiff|LINK
The int.Typarse , will only try to parse the integer, but it will not inform you whether the it was a success
It certainly will tell you if its a success or failure, thats why i have placed it within an if! int.TryParse will return true/false whether the conversion has been successful or not. In the case the conversion fails i simply returned 0. This could be
changed to display an error message to the user instead.
I agree client side validation would also be a good idea though :)
Dec 05, 2007 09:02 PM|nomadic1|LINK
Thank you all for your help. I have been on another project and I will try your answers over the weekend.