Help with preventing error in empty textbox for submitting quantity info
Help with preventing error in empty textbox for submitting quantity info
Help with preventing error in empty textbox for submitting quantity info
Help with preventing error in empty textbox for submitting quantity info
Help with preventing error in empty textbox for submitting quantity info
Help with preventing error in empty textbox for submitting quantity info Help with preventing error in empty textbox for submitting quantity info Help with preventing error in empty textbox for submitting quantity info Help with preventing error in empty textbox for submitting quantity info Help with preventing error in empty textbox for submitting quantity info Help with preventing error in empty textbox for submitting quantity info Help with preventing error in empty textbox for submitting quantity info Help with preventing error in empty textbox for submitting quantity info
Help with preventing error in empty textbox for submitting quantity info Help with preventing error in empty textbox for submitting quantity info
Help with preventing error in empty textbox for submitting quantity info
Go Back  Xtreme Visual Basic Talk > > > Help with preventing error in empty textbox for submitting quantity info


Reply
 
Thread Tools Display Modes
  #1  
Old 10-26-2006, 11:54 AM
easyeman easyeman is offline
Regular
 
Join Date: Nov 2005
Posts: 95
Question Help with preventing error in empty textbox for submitting quantity info


I'm having a slight problem I'm trying to prevent this error for inputting data into a textbox...I can probably figure it out but I just want to know what would be the best way to handle it.

Here's what I'm doing...I have a webpage set up that allows a customer to submit an order for calendars. What they do is go on and input the amount of Wall calendars they want into a textbox and then the amount of pocket calendars, along with other info such as the name, phone number, email, etc. I could probably just do what the other fields do in that it just pops up an error in a label saying something was not entered if a customer tries to submit the order without filling in all the info.

The problem I'm having is that well people obviously assume that if you leave a textbox blank for the quantity you want to order, that it will be 0. For example, if the customer just wanted 3 walls and no pocket, he would most likely put 3 for wall and leave the pocket textbox blank. Well with how my code is set up, if there is nothing in the field it just causes a server error and kills the page. The error says this:

Code:
 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 89:                     End If
Line 90: 
Line 91:                     If CShort(tbPocket.Text) > 0 Then
Line 92:                         cart.AddItem(cartId, "PCALENDAR", "POCKET CALENDAR", CShort(tbPocket.Text), "1.00", 0.2, "N", "Y", 0.0)
Line 93:                     End If
Here is the section of code that is giving an if statement for input entered which I guess I need to update the code (it is in the code behind file for the calendars aspx page.

Code:
If CShort(tbLarge.Text) > 0 Then
                        cart.AddItem(cartId, "CALENDAR", "LARGE CALENDAR", CShort(tbLarge.Text), "5.00", 1.0, "N", "Y", 0.0)
                    End If

                    If CShort(tbPocket.Text) > 0 Then
                        cart.AddItem(cartId, "PCALENDAR", "POCKET CALENDAR", CShort(tbPocket.Text), "1.00", 0.2, "N", "Y", 0.0)
                    End If
I guess my question is what should I do in the code to tell the page that if a customer just leaves the field blank and submits it, that it will default the null value or empty textbox to be 0 so that it does go through and only enters what was entered.

I guess if anything I could just set an error so that they can't leave the textbox blank...such as right now it does not test if it is blank but if there is a number in there it tests if it falls into the range of 0-9...so I need to set up something to see if it is blank. That probably wouldn't be hard just to have an error display in a label saying something like "you cannot leave the box blank, please enter a value of 0-9". However I guess it would be nicer just to let a customer submit an order even if the textbox quantity is blank, since I know most people would not think to enter 0 and would just assume leaving it blank means it would be 0.

Hope that makes sense and I appreciate the help! I'm trying to figure out how to best fix that but cannot think of what to put in the code.


And I just thought of another idea...would it be easier just to have a Dropdown listbox for that instead of text? I guess I never thought of that but it does make more sense since they can only order 0-9, and that way they can just choose the amount and I wouldn't to worry about a wrong amount being entered or the box being empty. It could just default to "0" in the selection and then they choose. Would that be better and if so how would you suggest I set that up in place of the textbox? Thank!!
Reply With Quote
  #2  
Old 10-26-2006, 01:32 PM
Nayru's Avatar
Nayru Nayru is offline
Centurion
 
Join Date: May 2006
Location: GTA
Posts: 119
Default

You could try testing the length of the textbox text rather than the value

Code:
If CShort(tbPocket.Text) > 0  and tbPocket.Text.length > 0 Then
                        cart.AddItem(cartId, "PCALENDAR", "POCKET CALENDAR", CShort(tbPocket.Text), "1.00", 0.2, "N", "Y", 0.0)
                    End If
That way if nothing was entered it will not be processed. A drop down list will give you more control over what the user can enter though, I typically stick with dropdown lists where ever possible to try and control input.

Without knowing the language you're using I can't really suggest anything about implementing the drop down lists.
Reply With Quote
  #3  
Old 10-26-2006, 01:54 PM
wayneph's Avatar
waynephHelp with preventing error in empty textbox for submitting quantity info wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

Nayru, your code will still crash and have the same problem. The CShort() will still run regardless of the other condition. If you want to do it that way, test the length first, and use the AndAlso operator to Short Circut the rest of the operation. That way CShort won't be called for empty strings.

I would personally use the TryParse() method of the integer class. That way you can make sure that the user entered a number. (I suspect it would crash if the user put in a letter as well.)

Code:
Dim cnt As Integer If Integer.TryParse(tbPocket.Text, cnt) Then 'cnt will be populated with the value if valid cart.AddItem(cartId, "PCALENDAR", "POCKET CALENDAR", cnt, "1.00", 0.2, "N", "Y", 0.0) End If

If you want, you could use a dropdown list, but then you'd have problems if someone wanted more items than you have available in your list.
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #4  
Old 10-26-2006, 04:36 PM
easyeman easyeman is offline
Regular
 
Join Date: Nov 2005
Posts: 95
Default

Quote:
Originally Posted by wayneph
Nayru, your code will still crash and have the same problem. The CShort() will still run regardless of the other condition. If you want to do it that way, test the length first, and use the AndAlso operator to Short Circut the rest of the operation. That way CShort won't be called for empty strings.

I would personally use the TryParse() method of the integer class. That way you can make sure that the user entered a number. (I suspect it would crash if the user put in a letter as well.)

Code:
Dim cnt As Integer If Integer.TryParse(tbPocket.Text, cnt) Then 'cnt will be populated with the value if valid cart.AddItem(cartId, "PCALENDAR", "POCKET CALENDAR", cnt, "1.00", 0.2, "N", "Y", 0.0) End If

If you want, you could use a dropdown list, but then you'd have problems if someone wanted more items than you have available in your list.
Thanks I'll try that out and see how it works. I'll try to get the textbox thing working and once I do, THEN I'll mess around and see if I can get it working with a dropdown list. There probably won't be a problem since with this item we are trying to limit the amount people can get, so we have a limit of no more than 9 per customer, so a dropdown list may work well as an option. Otherwise, the textbox should be good. Thanks for the help!
Reply With Quote
  #5  
Old 10-26-2006, 04:37 PM
easyeman easyeman is offline
Regular
 
Join Date: Nov 2005
Posts: 95
Default

Quote:
Originally Posted by Nayru
You could try testing the length of the textbox text rather than the value

Code:
If CShort(tbPocket.Text) > 0  and tbPocket.Text.length > 0 Then
                        cart.AddItem(cartId, "PCALENDAR", "POCKET CALENDAR", CShort(tbPocket.Text), "1.00", 0.2, "N", "Y", 0.0)
                    End If
That way if nothing was entered it will not be processed. A drop down list will give you more control over what the user can enter though, I typically stick with dropdown lists where ever possible to try and control input.

Without knowing the language you're using I can't really suggest anything about implementing the drop down lists.
haha oh sorry I forgot to specify. I'm using Visual Basic.NET with ASP.NET. I guess I assumed most of the stuff on this forum was with VB.NET so I didn't mention it. But that's what I am using.
Reply With Quote
  #6  
Old 10-26-2006, 05:18 PM
easyeman easyeman is offline
Regular
 
Join Date: Nov 2005
Posts: 95
Default

Hey! It's working! haha. yeah that parseInt seemed to do the trick and it's all working great. I already had some error catching statements in the code to take care of letters, so even if they type in a letter or multiple letters and numbers it won't submit and will tell them to enter a number from 0 through 10...so that's fine. The only thing that wasn't working was the blank textbox thing which the Int Parsing fixed, so I guess it's working great.

I probably could have set up another error catching statement to see if it was empty but this way is better for the customers so they don't HAVE to enter a number quantity for a certain calendar. I tested it out a few times and it just puts the quantity into the cart and gives no error. So I appreciate the help everyone! It's running great, and I may do dropdown list just to see if I get it working.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Help with preventing error in empty textbox for submitting quantity info
Help with preventing error in empty textbox for submitting quantity info
Help with preventing error in empty textbox for submitting quantity info Help with preventing error in empty textbox for submitting quantity info
Help with preventing error in empty textbox for submitting quantity info
Help with preventing error in empty textbox for submitting quantity info
Help with preventing error in empty textbox for submitting quantity info Help with preventing error in empty textbox for submitting quantity info Help with preventing error in empty textbox for submitting quantity info Help with preventing error in empty textbox for submitting quantity info Help with preventing error in empty textbox for submitting quantity info Help with preventing error in empty textbox for submitting quantity info Help with preventing error in empty textbox for submitting quantity info
Help with preventing error in empty textbox for submitting quantity info
Help with preventing error in empty textbox for submitting quantity info
 
Help with preventing error in empty textbox for submitting quantity info
Help with preventing error in empty textbox for submitting quantity info
 
-->