While trying to set Validations i initially encountered some problems with checking if a textbox is null, i tried using
private void btnGo_Click(object s
The System.String
data type in .NET is a class, a reference type. So an empty string (""
or string.Empty
) is a reference to a value with zero length, while null
does not reference a to real value, so any attempt to access the value it references will fail.
For example:
string emptyString = "";
string nullString = null;
Console.WriteLine(emptyString.Length); // 0
Console.WriteLine(nullString.Length); // Exception!
I'd recommend you use IsNullOrEmpty (or IsNullOrWhiteSpace) in your validation code, to handle both cases:
if (string.IsNullOrEmpty(name))
{
labelError.Visiblle = true;
labelError.Text = "Field Cannot be Left Blank"
}