0

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?

7
  • 2
    First problem: with all of those conversions on a single line, it's considerably harder to tell which text box doesn't have the right value. One of the first things you should do is separate them out. Next, log the value before you parse it - then you should be able to tell us exactly which value had a problem. Commented Jun 9, 2014 at 16:35
  • Good points for tesing, working on it will update code. Commented Jun 9, 2014 at 16:43
  • Really good tip to separate the conversions, I found that the conversion in question is the TotalBox which gives me a place to proceed. I am auto totalling the TotalBox in some javascript, and that works, but something else must be going astray. Commented Jun 9, 2014 at 16:48
  • Okay, the error is completely unrelated to my totalling of the box or my conversion. It was because my TotalBox was grayed out, in other words its "Enabled" property was set to false. Then I was automatically updating its value with Javascript. Basically, the user couldn't change their TotalBox value since it was a simple total. But by disabling the TotalBox, this caused the value of the TotalBox to not be available at runtime. Commented Jun 9, 2014 at 16:54
  • Musicode, I hope you will still consider breaking out the conversions. Commented Jun 9, 2014 at 16:55

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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


......

4 Comments

I have read on this, but would this solve my issue? Once again, I am 100% sure I am entering integers into each box when testing (just trust me, I am :p). So if it is failing right now, surley the TryParse calls will fail as well?
If all the values are integers then this is not the reason that it is failing (but I guess you already knew that) This approach may help identify and problems and is a better way to parse text values for conversion.
While it might not be your problem, it's just good coding practice. Never underestimate the end users ability to do the exact wrong thing. Can't have an exception popping up every time the user makes a mistake now can we? :)
Perhaps @Karli Rausepp instead of complaining about the code of others you bless us with your superior intelligence(%$#^%$#) and show us how you would do it. Nobody likes complainers...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.