I observed a strange problem in a piece of code where an adhoc SQL query was not producing the expected output, even though its parameters matched records in the data source. I decided to enter the following test expression into the immediate window:
new SqlParameter("Test", 0).Value
This gave a result of null
, which leaves me scratching my head. It seems that the SqlParameter
constructor treats zeroes as nulls. The following code produces the correct result:
SqlParameter testParam = new SqlParameter();
testParam.ParameterName = "Test";
testParam.Value = 0;
// subsequent inspection shows that the Value property is still 0
Can anyone explain this behaviour? Is it somehow intentional? If so, it's potentially rather dangerous...