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.Form
collection 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
Length
property of the string is zero.If the input field doesn't exist, you get a null reference (
Nothing
in VB) when reading from the collection, so to check for that you compare the reference tonull
(useis Nothing
in VB).To check for both conditions you can use the
String.IsNullOrEmpty
method. -
If Request.Form("myInput") IsNot Nothing Then Response.Write(Request.Form("myInput").ToString()) End If
Wrap 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