These days I am running into many strange scenarios :-)
I have tested the following two codes in ASP.Net application. Code 1 throws exception (as expected) whereas Code 2 does not throw exception (contrary to expectation) when the string value is null.
- Why is “EVAL” not throwing exception in this case?
- Will EVAL work always like this (i.e., “no exception”) or is it just a luck?
- Is there any MSDN reference that says 'Eval' returns empty string?
//Code 1: Causes Exception
string test = Convert.ToString(subscriber.EncryptedSSN).Substring(0, Convert.ToString(subscriber.EncryptedSSN).Length >= 5 ? 5 : Convert.ToString(subscriber.EncryptedSSN).Length);
// Code 2: Does not throw Exception
<%# Convert.ToString(Eval("EncryptedSSN")).Substring(0, Convert.ToString(Eval("EncryptedSSN")).Length >= 5 ? 5 : Convert.ToString(Eval("EncryptedSSN")).Length) %>
References: