I got this example from a book and found an error, if I leave the 4 input box (age) blank. Not sure why.
The test is what is giving me the error. It checks to see if not blank and if between 2 numbers. Why would this be a problem?
The test is what is giving me the error. It checks to see if not blank and if between 2 numbers. Why would this be a problem?
Code:
<%@Page Explicit="True" Language="VB" Debug="True" %>
<html>
<head/>
<script runat="server">
Sub CreateButton_Click(Sender As Object, E As EventArgs)
Dim AllOK As Boolean = True
NameLabel.Text = ""
PasswordLabel.Text = ""
ConfirmLabel.Text = ""
AgeLabel.Text = ""
Status.Text = ""
If Name.Text = "" Then
NameLabel.Text = "** Please Enter a Username"
AllOK = False
End If
If Password.Text = "" Then
PasswordLabel.Text = "** Please Enter a Password"
AllOK = False
End If
If Confirm.Text = "" Then
ConfirmLabel.Text = "** Please Confirm Your Password"
AllOK = False
End If
If Password.Text <> Confirm.Text Then
PasswordLabel.Text = _
"** Your Password and Confirmation Don't Match"
AllOK = False
End If
If Age.Text <> "" And (Age.Text < 18 Or Age.Text > 120) Then
AgeLabel.Text = _
"** Please Enter an Age Between 18 and 120"
AllOK = False
End If
If AllOK = True Then
Status.Text = _
"Everything Looks Good - Creating New Account"
Create.Enabled = False
' Create new user account
End If
End Sub
</script>
<body>
<h1>Create a New Account</h1>
<form runat="server">
Enter a Username:<br>
<asp:textbox id="Name" runat="server" />
<asp:label id="NameLabel" runat="server"
forecolor="red" /><br>
Enter a Password:<br>
<asp:textbox id="Password" textmode="password"
runat="server" />
<asp:label id="PasswordLabel" runat="server"
forecolor="red" /><br>
Confirm Password:<br>
<asp:textbox id="Confirm" textmode="password"
runat="server" />
<asp:label id="ConfirmLabel" runat="server"
forecolor="red" /><br>
Enter Your Age:<br>
<asp:textbox id="Age" runat="server" />
<asp:label id="AgeLabel" runat="server"
forecolor="red" /><br><br>
<asp:button text="Create" id="Create"
onclick="CreateButton_Click" runat="server"/><br>
<asp:label id="Status" runat="server" /><br>
</form>
</body>
</html>
Code:
Server Error in '/' Application.
--------------------------------------------------------------------------------
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.
Source Error:
Line 30: End If
Line 31:
Line 32: [COLOR=red]If Age.Text <> "" And (Age.Text < 18 Or Age.Text > 120) Then[/color]
Line 33: AgeLabel.Text = _
Line 34: "** Please Enter an Age Between 18 and 120"
Source File: c:\inetpub\[URL unfurl="true"]wwwroot\oldway.aspx[/URL] Line: 32
Stack Trace:
[FormatException: Input string was not in a correct format.]
Microsoft.VisualBasic.CompilerServices.DoubleType.Parse(String Value, NumberFormatInfo NumberFormat) +195
Microsoft.VisualBasic.CompilerServices.DoubleType.FromString(String Value, NumberFormatInfo NumberFormat) +84
[InvalidCastException: Cast from string "" to type 'Double' is not valid.]
Microsoft.VisualBasic.CompilerServices.DoubleType.FromString(String Value, NumberFormatInfo NumberFormat) +173
Microsoft.VisualBasic.CompilerServices.DoubleType.FromString(String Value) +7
ASP.OldWay_aspx.CreateButton_Click(Object Sender, EventArgs E) in c:\inetpub\[URL unfurl="true"]wwwroot\oldway.aspx:32[/URL]
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1277
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573