Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, January 16, 2008 6:59 AM | 1 vote
Hi,
Can any one rectify the following error:
TextBox11.Text = CDec(TextBox5.Text) + CDec(TextBox7.Text)
While i was calculating the values i receieved the following error:
{"Conversion from string "" to type 'Decimal' is not valid."}
All replies (8)
Wednesday, January 16, 2008 9:05 AM âś…Answered | 1 vote
Yes.. Allen is right..
The error will arise when there is no value in the text box.. or the value is not a valid decimal value..
If you consider the value inside the text box is not a valid decimal then it should be consider as zero. then you can do like this..
TextBox11.Text = CDec(IIf(Decimal.TryParse(TextBox5.Text, Nothing), TextBox5.Text, "0")) + CDec(IIf(Decimal.TryParse(TextBox7.Text, Nothing), TextBox7.Text, "0"))
And it is better to restric your text box to allow only decimal value.. not any invalid data..
Wednesday, January 16, 2008 8:49 AM | 1 vote
Hello,
There is no error in the code. However, this error can come up when there is no value in either of the Input Text boxes.
If your application have the option that one of the text boxes may not have a value and you still continue with the
addition considering the null value as 0, then you will have to put special validation on the Textboxes. You can check
before addition that if the textboxes do not have values, then only 0 will be sent as its value.
I hope this helps.
Regards,
Allen Smith
Software Engineer
ComponentOne LLC
Wednesday, January 16, 2008 9:06 AM
Yes, absolutely you are correct. Actually i was inserting value is another textbox rather than(CDec(TextBox5.Text)), So textbox5 was blank.
Thanks for reply.
Regards
Kashif Chotu
Wednesday, January 16, 2008 9:09 AM
Hi Prasant,
Code Block
TextBox11.Text = CDec(IIf(Decimal.TryParse(TextBox5.Text, Nothing), TextBox5.Text, "0")) + CDec(IIf(Decimal.TryParse(TextBox7.Text, Nothing), TextBox7.Text, "0"))
I feel better to use the above code at least i can get zero.
Thanks.
Wednesday, January 16, 2008 9:17 AM
Yes .. if your input on any one of the text box is empty or invalid then it will return 0. and It more better if you validate the key stroke in the text box by restricing it in the KeyDown event of the text box..
Wednesday, January 16, 2008 9:24 AM | 1 vote
Hi Prasant,
By the way i am using the following. It just a sample and later i will convert into "Decimal.TryParse".
Private Sub TextBox8_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox8.TextChanged, TextBox12.TextChanged
If IsNumeric(TextBox8.Text) AndAlso IsNumeric(TextBox12.Text) Then
TextBox7.Text = CDec(TextBox8.Text) + ((CDec(TextBox12.Text)) * 2)
TextBox11.Text = CDec(TextBox5.Text) + CDec(TextBox7.Text)
End If
End Sub
Thanks.
Wednesday, January 16, 2008 9:29 AM
Yes.. you can do like that .. but you are still missing to check the value in TextBox5.Text.. that may generate error if you leav e it blank..
Wednesday, January 16, 2008 9:44 AM
Hi Prasant,
I will check this one.
Thanks for providing useful information.
Regards,
Kashif Chotu