Hi,
When I use Request.Form("myInput") and the input field "myInput" is blank, I get a server error.
How do I handle this?
Is there a way to check if "myInput" has not been filled?
-
Reading from the
Request.Formcollection doesn't cause an exception neither if the value is an emptry string (which happens if the input field is blank) nor if the field doesn't even exist.If the input field is blank, you get an empty string when reading from the collection, so to check for that you just check if the value of the
Lengthproperty of the string is zero.If the input field doesn't exist, you get a null reference (
Nothingin VB) when reading from the collection, so to check for that you compare the reference tonull(useis Nothingin VB).To check for both conditions you can use the
String.IsNullOrEmptymethod. -
If Request.Form("myInput") IsNot Nothing Then Response.Write(Request.Form("myInput").ToString()) End IfWrap your code in an If statement to see if you are returning a null from the form. If you try to cast a null ToString() it will throw an exception.
-
You may try something like
If IsEmpty(Request.Form("myInput")) Then // input is empty, display error Else // input has been filled, continue End If
0 comments:
Post a Comment