Wrox Programmer Forums
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 15th, 2004, 11:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default Validating user input

I have a function like this:

Private Function IsValidForm() As Boolean

        If Not IsNumeric(txtPCount.Text) Or txtPCount.Text > 46 Then
            MsgBox("Please enter a valid value for each field.", _
                    MsgBoxStyle.Exclamation, Me.Text)
            Return False
        Else
            Return True
        End If

End Function

When I enter text into the text box it says:
An unhandled exception of type 'System.InvalidCastException'

How would I fix this or is there a nice catch all Form_Error like the Page_Error in ASP.NET?

 
Old January 15th, 2004, 12:31 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Should it be:
Code:
if ( not IsNumeric(txtPCount.Text) or txtPCount.Text.Length > 46) then
  ..
Comparing a string against a number is probably giving you a problem. Also, to trap errors, you can use try..catch, there is a Page_Error event, there is an Application_Error event (in the global.asax), or you can display a custom error page.

You may want to check out my other thread at: http://p2p.wrox.com/topic.asp?TOPIC_ID=8551.

Brian
 
Old January 15th, 2004, 12:51 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Thanks for the reply.

I forgot to mention that I'm using windows forms. I had to separate the two statements like this to get it to work:

If (Not IsNumeric(txtPCount.Text)) Then
                MsgBox("Value must be a numeric value and cannot be blank.", _
                        MsgBoxStyle.Exclamation, Me.Text)
                Return False
ElseIf txtPCount.Text > 46 Then
                MsgBox("There are that many computers in the lab", MsgBoxStyle.Exclamation, Me.Text)
                Return False
Else
                Return True
            End If





Similar Threads
Thread Thread Starter Forum Replies Last Post
Validating Input from a Datasheet using VBA digby_dog Access VBA 1 October 6th, 2008 10:35 AM
Displaying user input macrocosm Beginning PHP 3 June 15th, 2006 09:30 PM
user password validating cooky4 VB How-To 15 May 4th, 2005 08:45 AM
validating input bukky VBScript 1 April 1st, 2004 04:30 AM
validating user input hosefo81 Javascript How-To 12 March 3rd, 2004 09:32 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.