I was doing something like this:
SqlParameter param = new SqlParameter("@Param", 0) { SqlDbType = SqlDbType.Int };
private void TestParam(SqlParameter param) {
string test = param.Value.ToString(); // Getting NullReferenceException here
}
But I stop getting the exception when I put it like this:
SqlParameter param = new SqlParameter("@Param", SqlDbType.Int) { Value = 0 };
private void TestParam(SqlParameter param) {
string test = param.Value.ToString(); // Everything OK
}
Can anyone tell me why SqlParameter assumes 0 is the same as null?
Edit: MSDN Explains this here: SqlParameter Constructor