Wrox Programmer Forums
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 13th, 2007, 12:42 PM
Authorized User
Join Date: Feb 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sum Textbox Values

I have approximately 15 textboxes that I want to sum together.

I have tried textbox1.Text + Textbox2.Text but this just concatenates the numbers together.

I can get this to work by creating mulitple variables of type integer and then setting each textbox value as a seperate variable and then adding the variables together but this seems excessive.
Old April 13th, 2007, 01:36 PM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

You need to cast the .Text value to an integer. There's nothing excessive about that. That's what you get with a strongly typed language.

Maybe to make it a bit easier to write and run, you could add all the textboxes to a generic list:

   List<TextBox> lstTextBoxes = new List<TextBox>();

then loop thru the list to add everything up:

   int intTotal = 0;
   for(int i = 0; i < lstTextBoxes.Count; i++){
      intTotal += int.Parse(lstTextBoxes[i].Text);

Of course, you'll want to catch formatting errors and such.


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the Sum of all the values in a column.. jhansib4u ADO.NET 1 December 1st, 2007 09:15 PM
How Do I Sum Numbers In a TextBox wb8bgy Visual Basic 2005 Basics 1 December 10th, 2005 01:29 PM
Need help to write codes to sum values sunny76 Excel VBA 1 July 6th, 2005 05:05 AM
SUM with formula values SiliconFuRy Excel VBA 3 January 7th, 2005 08:17 AM
Sum values by key Justine Oracle 1 April 28th, 2004 03:12 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.