I'm trying convert value passed via text field to decimal and getting an error in doing so. I'm using vb .net trying to process gift certificates using web api. Any suggestions? Dim bal as decimal bal = Convert.ToDecimal(txtField.Text)
I'm trying convert value passed via text field to decimal and getting an error in doing so. I'm using vb .net trying to process gift certificates using web api. Any suggestions?
Dim bal as decimal
bal = Convert.ToDecimal(txtField.Text)
Can you post the exception message your getting? Also are you getting the message when your trying to comiple the code or when your running it?
Jon
If thats the code your using character by character I think you should try the following instead:
Dim bal As Decimal bal = Convert.ToDecimal(txtField.Text) The difference is As rather than as and Decimal rather than decimal .
The difference is As rather than as and Decimal rather than decimal .
I'm using correct case, vs web edition 08 makes all corrections for me
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.FormatException: Input string was not in a correct format.
--Ruslan
VB.NET isn't case sensitive, but Intellisense will set declarations to capitalize
Try this then:
Dim bal As Decimal bal = Convert.ToDecimal(txtField.Text.ToString)
Or --------------
Try Dim bal As Decimal bal = Convert.ToDecimal(txtField.Text.ToString)
Catch exception as System.FormatException Response.Write(“String is not formatted as a decimal”) Catch exception as System.ArgumentException Response.Write(“String is null”)End Try
You can use Decimal.TryParse to see if the parse of the text data succeeds. Then handle both when it succeeds and fails.
yu are correct its not recognizing it: "String is not formatted as a decimal "
Stack Trace:
[FormatException: Input string was not in a correct format.] System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +7469351 System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt) +146 System.Convert.ToDecimal(String value) +83 _Default.btnRedeem_Click(Object sender, EventArgs e) +89 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
Where does the value in the text field come from - are you populating that from an API call or is it user input?
You could try appending your txtField.Text.ToString to be
Trim(txtField.Text.ToString) -
there may be invisible characters there that could be tripping ToDecimal up.
Also, make sure that a $ is not in the value being sent - the value should only be numbers and the decimal.
Hi Susan,
Good day.
1, you can use a label to check out what is in txtField.
lblLabel.text="Start"+txtField.text.tostring() +"End"
2, if it is a user input, you can use RegularExpressionValidator to prevent errors.
3, if it comes from webapi, you can use substring to cut off unwanted part.
have fun
Ben
Thank you, this worked.