简体   繁体   中英

Vb.net Simple TextBox conversion to integer gives error input string was not in a correct format

I have some text boxes on a form I would like to validate. I want to confirm that they contain only numbers, and that the "total" text box is equal to the sum of the other boxes:

Protected Sub TotalBoxValidator_ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles TotalBoxValidator.ServerValidate
    If ((Convert.ToInt32(Box1.Text) + Convert.ToInt32(Box2.Text) + Convert.ToInt32(Box3.Text) + Convert.ToInt32(Box4.Text) + Convert.ToInt32(Box5.Text) + Convert.ToInt32(Box6.Text) + Convert.ToInt32(Box7.Text)) = Convert.ToInt32(TotalBox.Text)) Then
        args.IsValid = True
    Else
        args.IsValid = False
    End If
End Sub

Now during testing, I fill out the boxes with only integers. The total box is automatically updated to be the sum of the other boxes. However, when I click my "Submit" button on the form, and all the validators run, this one does not work properly.

The code throws an error on the first if statement, saying that the input to the Convert.ToInt32 calls is invalid. "input string was not in a correct format". I am positive that I am putting integers in each box, and that my TotalBox is equal to the sum of the other boxes. So I'm kind of stuck on why this would throw an error. Perhaps I am doing my conversion from string to integer wrong?

Does anyone have any idea what is going on here?

My issue had to do with the fact that my TotalBox (an ASP.net TextBox control) had its "Enabled" property set to false. I did this because it made the textbox greyed out, and the user could not change the value of the text box. Because the TextBox was not Enabled, I could not access the value of the TextBox at runtime. Though I was successfully changing its value in the page's javascript, and visually the value of the textbox was updating on the page, trying to use its value as a string in the code behind caused the error.

It did not have to do with the logic of the code or any syntax error. To fix this problem, I set the Enabled property of the TotalBox to true. Now the box is no longer greyed out, and user has the ability to change their total to be different than the sum of the boxes, which is not exactly what I want, but then again I am validating the total here so it is not a big deal. I will go back and try to grey out the textbox in javascript instead, because I have a feeling this will maintain the ability to get the TextBoxes value, while still having it be greyed out.

Edit: The above solution did work. For others experiencing this problem, consider trying to grey out the textbox using javascript, instead of any .net or asp code.

Here is the updated code:

Protected Sub TotalBoxValidator_ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles TotalBoxValidator.ServerValidate
        Dim Box1Value = Convert.ToInt32(Box1.Text)
        Dim Box2Value = Convert.ToInt32(Box2.Text)
        Dim Box3Value = Convert.ToInt32(Box3.Text)
        Dim Box4Value = Convert.ToInt32(Box4.Text)
        Dim Box5Value = Convert.ToInt32(Box5.Text)
        Dim Box6Value = Convert.ToInt32(Box6.Text)
        Dim Box7Value = Convert.ToInt32(Box7.Text)
        Dim TotalBoxValue = Convert.ToInt32(TotalBox.Text)
        If (Box1Value + Box2Value + Box3Value + Box4Value + Box5Value + Box6Value + Box7Value = TotalBoxValue) Then
            args.IsValid = True
        Else
            args.IsValid = False
        End If
    End Sub

Now, here is what was really helpful for debugging this situation. Try putting your conversions on different lines, because then when the program halts, it shows you exactly which conversion is going wrong. In my case, I got an error on the TotalBoxValue line. So I knew that something was going wrong with my TotalBox. What was different with the TotalBox than any of the other text boxes? Well, first of all, it was the total. But I knew this was working correctly. So then I thought about its properties. Its enabled property was set to false, different from the other boxes. So i knew this had to be changed.

For some reason in asp.net, when a control has its Enabled property set to false, you cannot access any of its properties at runtime. This is something I have noticed that is maybe not intuitive.

I suggest you use CompareValidators on your textboxes.

This will assure that the page won't even postback if your values aren't appropriate. Example markup:

<asp:CompareValidator ErrorMessage="errormessage" ControlToValidate="Box1" 
runat="server" Type="Integer" />

Then you could technically keep the code you've already posted, though if you wanted to forego the validators something like this would treat invalid input as 0:

Dim temp As Integer
Dim boxTotal = {box1.Text, _
                box2.Text, _
                box3.Text, _
                box4.Text, _
                box5.Text, _
                box6.Text, _
                box7.Text} _
              .Select(Function(text) If(Integer.TryParse(text, temp), temp, 0)) _
              .Sum()
Integer.TryParse(TotalBox.Text, temp)
args.IsValid = boxTotal = temp

You are attempting to convert a text value to an integer before knowing if the text value is an integer, please read up on

integer.TryParse

dim v1, v2, v3, v4, v5, v6, v7 as integer

if not integer.tryparse(box1.text, v1) then
     args.IsValid = False
     return
endif

if not integer.tryparse(box2.text, v2) then
     args.IsValid = False
     return
endif


......

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM