Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 15th, 2004, 11:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,111
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?

Reply With Quote
  #2 (permalink)  
Old January 15th, 2004, 12:31 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
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
Reply With Quote
  #3 (permalink)  
Old January 15th, 2004, 12:51 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,111
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:45 PM.


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