Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string format error

Status
Not open for further replies.

tfstom

Programmer
Sep 28, 2002
190
0
0
US
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?

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
 
you can't use operators to compare strings, you can use (age.text.length > 0) this wil check and see if you've typed anything or you need to use the "string.compare" function

You can better regret that you have done something than regret the fact you done nothing
 
Change:

If Age.Text <> "" And (Age.Text < 18 Or Age.Text > 120) Then

To:

If Age.Text <> "" And (CType(Age.Text ,Integer) < 18 Or CType(Age.Text ,Integer) > 120) Then

You were comparing text with integers. You need to convert the text to an integer to be able to compare them.

Hope this helps.

Hope everyone is having a great day!

Thanks - Jennifer
 
I only can seem to get the program to work if I put a value in the age field. With the above suggestions, I still get:

Code:
Exception Details: System.FormatException: Input string was not in a correct format.


Source Error: 

Line 30: End If
Line 31: 
Line 32: If Age.Text <> "" And ((Ctype(Age.Text,Integer)) < 18 Or (Ctype(Age.Text,Integer)) > 120) Then
Line 33:    AgeLabel.Text = _
Line 34:       "** Please Enter an Age Between 18 and 120"
Code:
Exception Details: System.FormatException: Input string was not in a correct format.

Source Error: 

Line 30: End If
Line 31: 
Line 32: If ((Ctype(Age.Text,Integer)) < 18 Or (Ctype(Age.Text,Integer)) > 120) Then
Line 33:    AgeLabel.Text = _
Line 34:       "** Please Enter an Age Between 18 and 120"
Code:
Exception Details: System.FormatException: Input string was not in a correct format.

Source Error: 


Line 30: End If
Line 31: 
Line 32: If (Age.Text.length <> 0) And ((Ctype(Age.Text,Integer)) < 18 Or (Ctype(Age.Text,Integer)) > 120) Then
Line 33:    AgeLabel.Text = _
Line 34:       "** Please Enter an Age Between 18 and 120"
 
Try
Code:
If Age.Text <> "" AndAlso ((Ctype(Age.Text,Integer)) < 18 Or (Ctype(Age.Text,Integer)) > 120) Then

I'm not very up with VB.NET prefering the C# flavor myself. However I believe that when you do a comparison using the And operator in VB.NET it will always evaluate both conditions. Hence even if no value is supplied for Age.text it tries to convert it to an integer for comparison in the other conditon anyway causing your error.

AndAlso is what is known as a short-circuit operator which replicates the behavoir of && in C#. If the first comparison returns false then the if "short circuits" without making the second comparison.

There is an equivalent for Or as well which is OrElse. There are some people who would argue that you should always use these short circuit operators, I may be one of them ;-)

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
I think your error is because you must use number for your comparison. This should work.
At the the top of your page add
Code:
<%@ Import NameSpace="System.Text.RegularExpressions" %>
Then use this
Code:
Dim MyRegMatch As Match = Regex.Match(Age.Text.ToString, "\d")
If MyRegMatch.Success Then
    IF (Convert.ToInt32(Age.Text) < 18 Or Convert.ToInt32(Age.Text) > 120) Then
        AgeLabel.Text = _
        "** Please Enter an Age Between 18 and 120"
        AllOK = False
    End If
Else
    AgeLabel.Text = _
        "** Please Enter an Age Between 18 and 120"
        AllOK = False
End If
Marty

 
Try This:

If IsNumeric(age.Text) Then
If ((CType(age.Text, Integer)) < 18 Or (CType(age.Text, Integer)) > 120) Then
AgeLabel.Text = _
"** Please Enter an Age Between 18 and 120"
AllOK = False
End If
Else
AgeLabel.Text = _
"** Please Enter an Age Between 18 and 120"
AllOK = False
End If

Hope everyone is having a great day!

Thanks - Jennifer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top