0
votes

I've exhausted every possible way I can think of to do this and I keep receiving this error:

"Input string was not in a correct format." Source Error:

Line 75: Dim intChocolate As Integer

Line 76: Dim strChocolate As String = txtChocolate.Text

Line 77: intChocolate = Convert.ToInt32(strChocolate) <----------HiLighted Red!!

Line 78:

Line 79: Dim intRaspberry As Integer

So what my program is is a "Checkout Page" .aspx web page with several textboxes for quantities of donuts at various prices ($1, $2, $3). The page has 1 calculate button that is supposed to run a ".aspx.vb" code segment to read those textbox.text properties, store them to a string, and then Convert.ToInt32. Those integers will be stored, multiplied by their prices, and a Total Cost will be displayed on the page.

The ONLY problem I am having with this is simply getting that textbox.text string to convert to an integer.

My .aspx.vb code:

Protected Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click

    Dim gPrice As Decimal
    gPrice = 1
    Dim cPrice As Decimal
    cPrice = 1.5
    Dim rPrice As Decimal
    rPrice = 2
    Dim regPrice As Decimal
    regPrice = 1
    Dim iPrice As Decimal
    iPrice = 1.5
    Dim mPrice As Decimal
    mPrice = 2

    Dim intGlazed As Integer
    Dim strGlazed As String = txtGlazed.Text
    intGlazed = Convert.ToInt32(strGlazed)


    Dim intChocolate As Integer
    Dim strChocolate As String = txtChocolate.Text
    intChocolate = Convert.ToInt32(strChocolate)

    Dim intRaspberry As Integer
    Dim strRaspberry As String = txtRaspberry.Text
    intRaspberry = Convert.ToInt32(strRaspberry)

    Dim intRegular As Integer
    Dim strRegular As String = txtRegular.Text
    intRegular = Convert.ToInt32(strRegular)

    Dim intIced As Integer
    Dim strIced As String = txtIced.Text
    intIced = Convert.ToInt32(strIced)

    Dim intMocha As Integer
    Dim strMocha As String = txtMocha.Text
    intMocha = Convert.ToInt32(strMocha)


    txtSubtotal.Text = calc.CalculateSubtotal(intGlazed, intChocolate, intRaspberry, intRegular, intIced, intMocha, gPrice, cPrice, rPrice, regPrice, iPrice, mPrice)

I'm sure this is some conflict in the way that Strings/Integers or textbox.text properties are handled in VB vs .aspx, but I can't for the life of me figure out why I am getting this error.

Any help is greatly appreciated, thank you very much.

2
What do you want to do if txtChocolate.Text is empty or it's not a number?ekad
All of the textboxes are verified to be integers with a CheckInteger function, I just didn't include that code.PATOB

2 Answers

1
votes

Use Integer.TryParse instead like this:

Dim intGlazed as Integer = 0
Integer.TryParse(txtGlazed.Text,intGlazed )
0
votes

Interesting problem.

Normally, this seems to work fine for me:

Dim intChocolate as Long = Clng(txtChocolate.Text)

But, given the error, it looks like you have some kind of non-numeric input. A few things that you do to make sure that the textbox only contains a number are:

1) Make sure there are no white spaces:

txtChocolate.Text.Replace(" ", "")

Or

2) Add validation:

<asp:RegularExpressionValidator id="RegularExpressionValidator1"
                   ControlToValidate="txtChocholate"
                   ValidationExpression="\d+"
                   Display="Static"
                   EnableClientScript="true"
                   ErrorMessage="Please enter numbers only"
                   runat="server"/>

If you are still having trouble, try using the debugger to see what the exact value of the .Text property is. That might show any odd characters that are showing up that you need to make sure are filtered out.