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.