VB .net converting string to decimal

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)

 

Parents
  • 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 .



    [edited by: Jon Ballinger at 1:52 PM (GMT -6) on 5 Nov 2009]
  • I'm using correct case, vs web edition 08 makes all corrections for me

     

    Input string was not in a correct format.

    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



    [edited by: Susan Hayes at 2:23 PM (GMT -6) on 5 Nov 2009]
  • 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



    [edited by: Nathan Campbell at 2:43 PM (GMT -6) on 5 Nov 2009]
  • 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
    

    --Ruslan

  • 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
    

    --Ruslan

  • Where does the value in the text field come from - are you populating that from an API call or is it user input?

Reply Children