Probably a C# noob question, so don't flame me. I was trying to do this:
if (ConfigurationManager.ConnectionStrings["PrimaryConnectionString"].ConnectionString != null)
{
// ...
}
But I kept getting a System.NullReferenceException
. I thought since it returns a string that I could just check for null
and move on. It took me a while to find the right way to do it:
ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["PrimaryConnectionString"];
if (cs != null)
{
this.Connection.ConnectionString = cs.ConnectionString;
}
So in other instances, like checking a Session
object for some value I would do a check for null
like this:
if (Session["EmployeeID"] != null)
{
_EmployeeID = System.Convert.ToInt32(Session["EmployeeID"]);
}
So I just wanted to know how do you know when you can or can't do a check for null
?