Calculated value on textChanged event can't be updated to database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashukite
    New Member
    • Jan 2010
    • 14

    Calculated value on textChanged event can't be updated to database

    I have invoice form bound to a bindingsource and bindingnavigato r. In the form I have unit price, quantity and total textboxes. The total textbox text property is set to total.text = quantity.text * unitprice.text in the textChanged event of both quantity and unitprice text boxes. The result is displayed in the total textbox. But when i hit save button on the bindingnavigato r it is set to null and not saved to database.

    The code on save button is
    bindingsource.e ndEdit()
    bindingsource.u pdate()
    When I used msgbox to see the value of total textbox before bindingsource.e ndEdit() it is the correct value. but after the code bindingsource.e ndEdit() it is null. I checked the databinding property and it is correct. What is the problem.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    total.text = quantity.text * unitprice.text
    You can't apply math operations to text. You need to convert the text to some flavor of numbers first, whether it be floats, ints or decimals. Otherwise you might as be trying to perform total.text = "Fred" * "Wilma" which will not equal "Pebbles".

    Comment

    • ashukite
      New Member
      • Jan 2010
      • 14

      #3
      the text box doesn't accept text. it is validated to accept numeric values only. Besides I tried this
      total.text = convert.todecim al(quantity.tex t * unitprice.text) and the result I get on the msgbox is correct result. This is not the case.

      Comment

      • ashukite
        New Member
        • Jan 2010
        • 14

        #4
        i didn't solve this problem. but i deleted the total field from my databse because it is a calculated value. I added a column to my dataset and make its value an expression.

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          the text box doesn't accept text. it is validated to accept numeric values only. Besides I tried this
          total.text = convert.todecim al(quantity.tex t * unitprice.text) and the result I get on the msgbox is correct result. This is not the case
          The fact that you limit the incoming text to just numbers doesn't change the fact that the .Text value of a textbox is not an numeric value but a string.
          You have to convert each value to a decimal *THEN* you can multiply those numbers.
          Code:
          decimal NewValue = Convert.ToDecimal(quantity.text) * Convert.ToDecimal(unitprice.Text);

          Comment

          Working...