Reputation: 191
I have 8 textboxes that only accept integers. How to calculate the total of value entered by users in each textbox and the result will be showed as a label. After user click on submit button, the the value of the label(result of numbers) will be stored in db. Anyone knows? Thank you. Looking forward to hear from u guys. I got this error : Input string was not in a correct format. At this line:
int total = 0;
total = int.Parse(TextBox2.Text) + int.Parse(TextBox4.Text) + int.Parse(TextBox6.Text) + int.Parse(TextBox8.Text) + int.Parse(TextBox10.Text) + int.Parse(TextBox12.Text) + int.Parse(TextBox14.Text) + int.Parse(TextBox16.Text);
Label1.Text = total.ToString();
Upvotes: 0
Views: 23713
Reputation: 1021
I solved it in this way :
you can create a void then you can call this void any where in your program or any button.
void Calculate_Total()
{
double txt1 = Convert.ToDouble(txtBox1.Text);
double txt2 = Convert.ToDouble(txtBox2.Text);
double txt3 = Convert.ToDouble(txtBox3.Text);
double txt4 = Convert.ToDouble(txtBox4.Text);
double txt5 = Convert.ToDouble(txtBox5.Text);
double txt6 = Convert.ToDouble(txtBox6.Text);
double txt7 = Convert.ToDouble(txtBox7.Text);
double txt8 = Convert.ToDouble(txtBox8.Text);
double total = txt1 + txt2 + txt3 + txt4 + txt5 + txt6 + txt7 + txt8;
Label1.Text = Convert.Tostring(total);
}
Upvotes: 0
Reputation: 18064
You can do this way:-
totalValue = int.Parse(txtBox1.Text) + int.Parse(txtBox2.Text) + int.Parse(txtBox3.Text) + int.Parse(txtBox4.Text) + int.Parse(txtBox5.Text) + int.Parse(txtBox6.Text) + int.Parse(txtBox7.Text) + int.Parse(txtBox8.Text);
Store the value in label
label.Text = totalValue;
OR
foreach (Control c in this.Controls)
{
if (c.GetType().Name.ToString()=="TextBox")
{
totalValue += int.Parse(String.Format("txtBox{0}.Text", count));
}
}
foreach (Control c in this.Controls)
{
if (c.GetType().Name.ToString()=="TextBox")
{
int value = 0;
if(int.TryParse(((TextBox)c).Text,out value))
totalValue += value;
}
}
Store the value in label
label.Text = totalValue;
Upvotes: 2
Reputation: 1061
Answer to your second question is hold the total value in a parameter and get that value in some other page by using
Request.QueryString
for the previous page.. The second way is to keep the total value in a global(static) variable and use it in any of yours page.
Upvotes: 1
Reputation: 27
its simple all you have to do is convert all textbox text to double and than add them with FOR loop.
Upvotes: 2
Reputation: 887
If you can use jQuery add a common class to all the textboxes say "textToCalculate", if the Id of the label is "labelToShowResult"
<script type="text/javascript">
$(document).ready(function () {
$('.textToCalculate').change(function() {
var total = 0;
$('.textToCalculate').each(function(){
total += parseInt(this.value);
});
$('#labelToShowResult').val(total);
});
});
</script>
Hope this works
Upvotes: 3
Reputation: 5596
int total = 0;
total = int.Parse(txtBox1.Text) + int.Parse(txtBox2.Text) + int.Parse(txtBox3.Text) + int.Parse(txtBox4.Text) + int.Parse(txtBox5.Text) + int.Parse(txtBox6.Text) + int.Parse(txtBox7.Text) + int.Parse(txtBox8.Text);
Label1.Text = total.ToString();
Upvotes: 1