[RESOLVED] I can't opearte a simple + command...?-VBForums
Results 1 to 4 of 4

Thread: [RESOLVED] I can't opearte a simple + command...?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2010
    Posts
    110

    Resolved [RESOLVED] I can't opearte a simple + command...?

    This is a bit embarrassing...

    I can't manage to add different values together...

    My code:
    Code:
    TextBoxAnswer.Text = TextBox1.Text + TextBox2.Text + TextBox3.Text + TextBox4.Text
    I import decimal values from my SQL Server into the TextBoxes 1-4, which works fine. But when I try to add the values together into another textbox the answer is just the values combined like a string...?
    The column of the table in the SQL server has a datatype decimal(18, 2)

    I have also tried to convert the values in the textboxes into int, str, double... Nothing seems to work...
    Need some help here!


    Example:
    Answer = 2,50 + 2,50 + 5,50 + 4,50
    So,
    TextBoxAnswer.Text = 10,00

    but, all I get is:
    2,502,505,504,50

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: I can't opearte a simple + command...?

    Because you are "adding" Strings! You have your decimal values from database, however you put into textboxes (which hold text, or strings).
    You have to Re-convert the text to a number, or even better use the original number for the addition!
    The Re-Convert would look like that:
    Code:
    TextBoxAnswer.Text = CDbl(TextBox1.Text) + CDbl(TextBox2.Text) + CDbl(TextBox3.Text) + CDbl(TextBox4.Text)
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2010
    Posts
    110

    Re: I can't opearte a simple + command...?

    Quote Originally Posted by opus View Post
    Because you are "adding" Strings! You have your decimal values from database, however you put into textboxes (which hold text, or strings).
    You have to Re-convert the text to a number, or even better use the original number for the addition!
    The Re-Convert would look like that:
    Code:
    TextBoxAnswer.Text = CDbl(TextBox1.Text) + CDbl(TextBox2.Text) + CDbl(TextBox3.Text) + CDbl(TextBox4.Text)



    Thanks a lot! I appreciate this!





  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,600

    Re: I can't opearte a simple + command...?

    You might want to be sure beforehand that the values you are adding are really numbers (since textboxes can hold almost anything), using one way or other.

    If you intend to add and show the database fields, then use those instead of textbox values, since values in the textboxes can be altered.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." Charles F. Kettering

    Read articles on My Blog 101 LINQ Samples JSON Validator XML Schema Validator "How Do I" videos on MSDN VB.NET and C# Comparison Good Coding Practices VBForums Reputation Saver String Enum Super Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.